0

在我的小提琴中:http: //jsfiddle.net/qJdaA/2/ 此代码假设在#monitor没有选中复选框时禁用按钮。

        var checked = $('#status_table tr [id^="monitor_"]:checked');
        if (checked.index()==-1){
            $('#monitor').attr('disabled','true');

        }

但它似乎不起作用。怎么了?

编辑:我发现我的代码变得模糊了。为了简化整个事情,也许我应该改变逻辑:s 如果我只想在至少有一个复选框被选中的情况下启用监视器按钮,我应该怎么做?

4

2 回答 2

3

尝试使用此条件:

if (checked.length === 0) {

你应该使用.prop(), 而不是.attr()设置禁用状态(以及像checkedand之类的东西selected)。

所以它会是:

$("#monitor").prop("disabled", true); // (or false, to enable it)

参考:

于 2013-05-15T03:15:45.430 回答
1

尝试

$('#monitor').prop('disabled',true);

演示:小提琴

于 2013-05-15T03:15:48.287 回答