简体   繁体   English

检查是否已选择特定的单选按钮

[英]Check if a specific radio button has been selected Jquery

What I'm trying to accomplish is: 我想要完成的是:

When a user clicks the first Open until Filled option, leave the date input disabled. 当用户单击第一个“ 打开直到填充”选项时,请禁用日期输入。 But when the user clicks on the second Open Until radio button. 但是,当用户单击第二个“ 直到打开”单选按钮时。 Have the date input enabled. 启用日期输入。

As of right now I can't seem to get it working. 截至目前,我似乎无法正常工作。 It seems to skip my check altogether. 似乎完全跳过了我的检查。 Am I missing something? 我想念什么吗?

$(function() {
    if ($('input#id_open:checked').length > 0) {
        alert("please fill in the date");
        $('input[name=end_date]').attr('disabled', false);
    }
});

I made a fiddle here to show what I mean. 我在这里摆弄我的意思。

Thanks for your help in advance! 谢谢您的帮助!

Working example: http://jsfiddle.net/8KggV/5/ 工作示例: http : //jsfiddle.net/8KggV/5/

$(function() {
    $('input').click(function(){
    if ($('#id_open:checked').length > 0) {
        $('input[name=end_date]').attr('disabled', false);
    }
        else{
            $('input[name=end_date]').attr('disabled', true);        
        }
    })
});

when using jQuery, to detect if a control is checked I use the is method. 使用jQuery时,要检测是否检查了控件,我使用了is方法。 As well you don't need to check the length, as the is method return a boolean. 同样,您不需要检查长度,因为is方法返回一个布尔值。

$(function() {
    if ($('input#id_open').is(':checked')) {
        alert("please fill in the date");
        $('input[name=end_date]').attr('disabled', false);
    }
});

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

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