禁用复选框使用jquery不工作

disable checkbox using jquery not working

本文关键字:工作 jquery 复选框      更新时间:2023-09-26

我正在阅读一本关于Jquery的书,遇到了一个练习,但它不起作用,检查了stackoverflow上的一些相关问题,并与我到目前为止所做的交叉检查,它几乎是相同的,但我的不起作用,有人能提供一个解决方案吗?下面是代码:

<form action="#">
Billing Address same as Shipping Address:
<input type="checkbox" name="billingAddress" id="billingAddress" />
<br />
<br />
Street Address:
<input class="baddr" type="text" name="street" id="street" />
<br />
<br />
City:
<input class="baddr" type="text" name="city" id="city" />
</form>
<script type="text/jscript">
$(document).ready(function() {
    $("#billingAddress").click(function() {
        if ($("#billingAddress").attr("checked") == true) {
            alert("Id billingAddress is checked!");
            $(".baddr").val("");
            $(".baddr").attr('disabled', 'disabled');   
        } else if ($("#billingAddress").attr("checked") == undefined) {
            $(".baddr").removeAttr("disabled"); 
        }
    });
});
</script>

工作演示:有很多方法,其中之一是:http://jsfiddle.net/J9vWu/

  • 可以使用这个:http://api.jquery.com/checked-selector/
  • 也可以链接:)

使用this和链接一些东西的另一个版本:http://jsfiddle.net/z9xjB/或http://jsfiddle.net/NzEjK/

$(document).ready(function () {
    $("#billingAddress").click(function () {
        if ($("#billingAddress").prop("checked")) {
            alert("Id billingAddress is checked!");
            $(".baddr").val("");
            $(".baddr").prop('disabled', 'disabled');
        } else if ($("#billingAddress").prop("checked") == undefined) {
            $(".baddr").removeAttr("disabled");
        }
    });
});

$(document).ready(function () {
    $("#billingAddress").click(function () {
        if ($(this).prop("checked")) {
            alert("Id billingAddress is checked!");
            $(".baddr").val("").prop('disabled', 'disabled');;
        } else if ($(this).prop("checked") == undefined) {
            $(".baddr").removeAttr("disabled");
        }
    });
});