选择具有相同值的无线电输入时如何禁用具有特定值的无线电输入

时间:2019-05-26 01:03:17

标签: javascript jquery html

如果我检查名称为“ 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>

1 个答案:

答案 0 :(得分:2)

在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 + '"]')中串联字符串,您可以在上方看到代码段