1

我有一小段代码想用作 jQuery 对话框的条件。非常简单的情况,如果选中了复选框,则启用按钮,如果没有,则禁用它。下面的代码很好地禁用了按钮,但在选中任何复选框时都没有启用它。

我尝试了多种变体,但无法在我的代码中使用它(Fiddle 中的一些工作正常)。非常感谢您的帮助。

JS 片段

if (button.text() == "Apply") {
    if ($("selectLocation").is(':checked'))
        button.enabled(true); // checked
    else
        button.enabled(false);  // unchecked
}

HTML

<input type="checkbox" name="selectLocation" />
4

4 回答 4

2

您将不得不使用 Jquery 的 .prop() 来解决这个问题

if (button.text() == "Apply"){
    if ($("selectLocation").is(':checked')){
        button.prop('disabled', false);
    }
    else{
        button.prop('disabled', true);
    }
}

我从来没有能够让 .attr() 为这种情况工作

于 2013-07-15T14:39:58.110 回答
0

试试这样:

$("input[name=selectLocation]")

它不起作用,因为您选择的是带有标签的selectLocation元素而不是带有名称的元素selectLocation

于 2013-07-15T14:38:22.477 回答
0

如果按钮是一个 jquery 元素:

button.attr("disabled", "disabled"); //to disable it
button.removeAttr("disabled"); //to reenable it

在您的代码中,它应该是:

if (button.text() == "Apply") {
    if ($("selectLocation").is(':checked'))
        button.removeAttr("disabled"); //to reenable it
    else
        button.attr("disabled", "disabled");
}

选择器$("input[name=selectLocation]")应该可以工作

于 2013-07-15T14:38:37.450 回答
0

不确定 jQuery 是否会选择这样的名称,添加一个 ID 并更改选择器。

if (button.text() == "Apply") {
    if ($("#selectLocation").is(':checked'))
        button.removeAttr("disabled");
    else
        button.attr("disabled", "disabled");
}

<input id="selectLocation" type="checkbox" name="selectLocation" />
于 2013-07-15T14:38:38.833 回答