简体   繁体   English

选择“单选按钮”中的一个选项时如何禁用其他选项?

[英]How to disable other opitons when one option is selected of “radio buttons”?

I am trying to disable other options immediately once an option is selected but using java-script I could not do this.我试图在选择一个选项后立即禁用其他选项,但使用 java 脚本我无法做到这一点。 Please help me.请帮我。 I have tried so far到目前为止我已经尝试过

 <?php 
    $answer = $exm->getAnswer($number);

    if ($answer) {
        while ($result = $answer->fetch_assoc()) {
?>
            <tr>
                <td>
                 <input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>"/><?php echo $result['ans']; ?>



<script id="question_radio" type="text/javascript">$(":radio").click(function(){
   var radioName = $(this).attr("ans"); //Get radio name
  $(":radio[name='"+radioName+"']:not(:checked)").attr("disabled", true); //Disable all unchecked radios with the same name
});             

            </script>


                </td>
            </tr>
<?php } } ?>
            <tr>
              <td>
                <input type="submit" name="submit" value="Next Question"/>
                <input type="hidden" name="number" value="<?php echo $number; ?>" />

                </td>
            </tr>

You can get the name attribute of the radio which is clicked and depending on this we will loop through all the radio button with that name and disable radio button which are not checked.您可以获得被clicked的单选按钮的name属性,并根据此我们将遍历所有具有该名称的单选按钮并disable未选中的单选按钮。

Here is demo code:这是演示代码:

 $("input[type=radio]").click(function() { //getting name attribute if radio which is clicked var name = $(this).attr("name"); console.log(name) //loop only through those radio where name is same $('input[name="' + name + '"]').each(function() { //if not selected if ($(this).is(":not(:checked)")) { // add disable $(this).attr('disabled', 'disabled'); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>" /> A <input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>" />B <input id="question_radio" type="radio" name="ans" value="<?php echo $result['id']; ?>" />C <br> <input id="question_radio" type="radio" name="ans1" value="<?php echo $result['id']; ?>" />A <input id="question_radio" type="radio" name="ans1" value="<?php echo $result['id']; ?>" /> B <input id="question_radio" type="radio" name="ans1" value="<?php echo $result['id']; ?>" />C

Here is the implementation in pure Javascript这是纯 Javascript 中的实现

document.querySelectorAll("input[type=radio]").forEach(function (el) {
  el.addEventListener('click', function () {
    //getting name attribute if radio which is clicked
    var name = el.getAttribute('name');
    //loop only through those radio where name is same.
    document.querySelectorAll('input[name="' + name + '"]').forEach(function (el) {
      if (el.matches(":not(:checked)")) {
        el.setAttribute('disabled', 'disabled');
      }
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM