如果我检查了名称为“most1”且值相同的单选按钮,如何禁用名称为“least1”的单选按钮
<form action="{{route('questionnaire.action')}}" name="QuestionnaireForm" method="post" id="questionnaireEnglishForm">
<input type="radio" name="most1" value="Trusting & believing in others">
<input type="radio" name="least1" value="Trusting & believing in others">
<input type="radio" name="most1" value="Satisfied, easy to please">
<input type="radio" name="least1" value="Satisfied, easy to please">
<input type="radio" name="most1" value="Clear">
<input type="radio" name="least1" value="Clear">
<input type="radio" name="most1" value="Calm & Peaceful">
<input type="radio" name="least1" value="Calm & Peaceful">
</form>
最佳答案
在jQuery中
[1] change
可以使用 most1
事件
[2] 使用.filter()
按值过滤least1
[3] 使用 .prop('disabled' , true/false)
打开/关闭禁用属性
[4] 如果 .prop('checked' , false)
被选中,你可能需要 least1
来取消选中它
$('input[name="most1"]').on('change' , function(){
var GetValue = $(this).val();
$('[name="least1"]').prop('disabled' , false).filter(function(){
return $(this).val() == GetValue;
}).prop('disabled' , true).prop('checked' , false);
});
var i = 12;
console.log('[name="least' + i + '"]');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="{{route('questionnaire.action')}}" name="QuestionnaireForm" method="post" id="questionnaireEnglishForm">
<input type="radio" name="most1" value="Trusting & believing in others">
<input type="radio" name="least1" value="Trusting & believing in others">
<input type="radio" name="most1" value="Satisfied, easy to please">
<input type="radio" name="least1" value="Satisfied, easy to please">
<input type="radio" name="most1" value="Clear">
<input type="radio" name="least1" value="Clear">
<input type="radio" name="most1" value="Calm & Peaceful">
<input type="radio" name="least1" value="Calm & Peaceful">
</form>
在 javascript
$('[name="least' + i + '"]')
中连接字符串.. 你可以看到上面的代码片段