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

时间:2013-03-27 20:19:56

标签: jquery html radio-button

我想要完成的是:

当用户点击第一个打开直到填充选项时,请禁用日期输入。但是当用户点击第二个 Open Until 单选按钮时。启用 date 输入。

截至目前,我似乎无法让它发挥作用。它似乎完全跳过了我的检查。我错过了什么吗?

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

我做了一个fiddle here来表明我的意思。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

工作示例: 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);        
        }
    })
});

答案 1 :(得分:1)

使用jQuery时,为了检测是否检查了控件,我使用了is方法。同样,你不需要检查长度,因为is方法返回一个布尔值。

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