简体   繁体   English

启用/禁用jQuery中单选按钮选择的按钮

[英]Enable/Disable a button on select of radio button in jquery

I have a page in which there are 2 radio buttons and a next button. 我有一个页面,其中有2个单选按钮和一个next按钮。 I have made next button disabled and it is enabled only when I select any radio button. 我已禁用了下一个按钮,并且仅当我选择任何单选按钮时才启用。 But now when I go to another page and come back to this page the next button comes as disabled although the radio button is already selected. 但是现在当我转到另一个页面并返回到该页面时,尽管已选择了单选按钮,但下一个按钮被禁用。 PFB the code PFB代码

$(document).ready(function () {
     $('#commandButton_1_0').attr('disabled', 'true');
     $('input:radio[name=a_SignatureOption]').click(function () {
         var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
         if (checkval == '1' || checkval == '2') {
             $('#commandButton_1_0').removeAttr('disabled');
         }
     });
});

Try 尝试

$(document).ready(function() {
    $('input:radio[name=a_SignatureOption]').click(function() {
        var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
        $('#commandButton_1_0').prop('disabled', !(checkval == '1' || checkval == '2'));
    });
});

Demo: Fiddle 演示: 小提琴

On load of document you need to check whether radio button is clicked or not 在加载文档时,您需要检查是否单击了单选按钮

$(document).ready(function() {
  $('input:radio[name=a_SignatureOption]').each(function() {
         checked(this);
  });

  $('input:radio[name=a_SignatureOption]').click(function() {
    checked(this);
  });

  function checked(obj){
    if($(obj).is(':checked')) {
        $('#commandButton_1_0').removeAttr('disabled');
    }else{
        $('#commandButton_1_0').attr('disabled', 'true');
    }
  }
});

I took time to understand your problem, 我花时间了解你的问题,

This can be solved by unchecking the radio while loading the page . 这可以通过在加载页面时取消选中无线电来解决。

$(document).ready(function () {
    $('input:radio[name=a_SignatureOption]').prop('checked', false);
    $('#commandButton_1_0').attr('disabled', 'true');  
    $('input:radio[name=a_SignatureOption]').click(function () {
    var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
    if (checkval == '1' || checkval == '2') {
        $('#commandButton_1_0').removeAttr('disabled');
    }
});
});

check out JSFiddle , btw redirect to other site and press back button to find the difference. 签出JSFiddle ,然后重定向到其他站点,然后按返回按钮查找区别。

Hope you understand. 希望你能理解。

// Try this............. // 尝试这个.............

$(document).ready(function () {
$("input:radio[name=a_SignatureOption]").removeAttr('checked'); 
$('#commandButton_1_0').attr("disabled","disabled");

 $('input:radio[name=a_SignatureOption]').click(function () {
    var checkval = $('input:radio[name=a_SignatureOption]:checked').val();   
     if (checkval == '1' || checkval == '2') {

         $('#commandButton_1_0').removeAttr("disabled","disabled");
     }
 });

}); });

It is simple as your question is 很简单,因为您的问题是

<form name="urfrm">
<input type="radio" value="1" name="a_SignatureOption"> Yes<br>
<input type="radio" value="2" name="a_SignatureOption"> No<br>
<input type="submit" id="butn" name="butn" value="next" disabled><br>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    //This will check the status of radio button onload
    $('input[name=a_SignatureOption]:checked').each(function() {
        $("#butn").attr('disabled',false);
    });

    //This will check the status of radio button onclick
    $('input[name=a_SignatureOption]').click(function() {
        $("#butn").attr('disabled',false);
    });
});
</script>

The easiest solution I can think of - albeit somewhat belatedly, is: 我可以想到的最简单的解决方案-尽管有些迟了,但它是:

// selects all input elements, whose name is 'a_SignatureOption' and whose
// type is 'radio'
// binds a change event-handler, using 'on()':
$('input[name=a_SignatureOption][type="radio"]').on('change', function(){
    // sets the 'disabled' property of the button to 'true' if zero radio inputs
    // are checked, or to false if one is checked:
    $('#commandButton_1_0').prop('disabled', $('input[name=a_SignatureOption][type="radio"]:checked').length === 0);
// triggers the change event-handler on page load:
}).change();

References: 参考文献:

You forgot to check the buttons at the beginning. 您忘记了在开始时检查按钮。

$(document).ready(function () {
    $('#commandButton_1_0').attr('disabled', 'true');
    if( $('input[name=a_SignatureOption]:checked' ).size() > 0 ) {
       $('#commandButton_1_0').removeAttr('disabled');
    }
    $('input[name=a_SignatureOption]').click(function () {
        if( $('input:radio[name=a_SignatureOption]:checked' ).size() > 0 ) {
            $('#commandButton_1_0').removeAttr('disabled');
        }
    });
});

By the way, I always suggest to add classes to form elements and work with those, instead of using [name="..."]. 顺便说一句,我总是建议添加类以形成表单元素并使用它们,而不是使用[name =“ ...”]。 It's quicker and simplier and you can change input names (if necessary) without touching js 更快,更简单,您无需触摸js即可更改输入名称(如有必要)

    <html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>
    $(document).ready(function () {
        $('#commandButton_1_0').attr('disabled', 'true');

        $('input:radio[name=a_SignatureOption]').click(function () {
            var checkval = $('input:radio[name=a_SignatureOption]:checked').val();
            alert(checkval)
            if (checkval == '1' || checkval == '2') {
                $('#commandButton_1_0').removeAttr('disabled');
            }
            else {
                $('#commandButton_1_0').attr('disabled', 'disabled');
            }
        });
    });
</script>
</head>
<body>
  <input type="radio" name="a_SignatureOption" value="1" /> value1
    <br />
    <input type="radio" name="a_SignatureOption" value="2"/> value2
    <br />
    <input type="radio" name="a_SignatureOption" value="3" checked="checked"/> value3
    <br />
    <input type="button" id="commandButton_1_0" value="Next"/>
</body>
</html>

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

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