禁用复选框使用jquery无法正常工作

时间:2014-05-20 22:52:32

标签: javascript jquery

我正在阅读一本关于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>

1 个答案:

答案 0 :(得分:0)

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

另一个使用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");
        }
    });
});