1

我发布了以下代码,用于禁用表格的文本字段。现在如何修改它,使其也禁用字段的文本字段,例如 id=name=usn。

jQuery(document).ready(function ($) {
// Disabling Text Fields by default
$("table input[type='text']").each(function () {
    $(this).attr("disabled", "disabled");
});

$(".inputradio1 input[name=select1]").change(function () {
    var val = $(this).val();
    if (val == "No") {
        $("table input[type='text']").each(function () {
            $(this).attr("disabled", "disabled");
        });
    } else if (val == "Yes") {
        $("table input[type='text']").each(function () {
            $(this).removeAttr("disabled");
        });
    }
});
});
4

3 回答 3

2

使用 prop('disabled', true) 代替。自 jQuery 1.6/1.7 版本以来,为了区分属性和属性,发生了一些变化。此外,不需要循环,您可以将其应用于整个匹配元素集:

$("table input[type='text']").prop('disabled', true);

使用以下内容还将使您的选择器更快更短地阅读:

$("table :text").prop("disabled", true);

所以最后,你剩下的是:

$("table :text").prop("disabled", true);

$(".inputradio1 input[name=select1]").change(function () {
    $("table :text").prop('disabled', $(this).val() === 'No');
});
于 2013-07-27T16:16:32.430 回答
1

是的朋友我做到了...

$("table :text").prop("disabled", true);

$(".inputradio1 input[name=select1]").change(function () {
$("#usn, table input[name='usn']").prop('disabled', $(this).val() === 'No') && $("table :text").prop('disabled', $(this).val() === 'No');
});
于 2013-07-27T16:50:04.900 回答
1

我不确定我是否理解这个问题。

使用prop()代替。

禁用 ifidnameis usn

// id's are suppose to be unique. no need for complex find
$("#usn, table input[name='usn']").prop("disabled",true);

编辑:

jQuery(document).ready(function ($) {
    // Disabling Text Fields by default
    $("table input[type='text']").prop('disabled',true);
    $("#usn, table input[name='usn']").prop("disabled",true);

    $(".inputradio1 input[name=select1]").change(function () {
        val disable = $(this).val() == "No";
        $("table input[type='text']").prop("disabled",disable);
        $("#usn, table input[name='usn']").prop("disabled",disable);
    });
});
于 2013-07-27T16:18:52.677 回答