无线电 - 单击动作和长度动作

时间:2011-05-05 12:42:36

标签: javascript jquery javascript-events jquery-selectors

我在页面上有无线电按钮,当页面加载时,它会使用长度动作来检查然后隐藏/显示一些元素。

当用户点击收音机时,它会隐藏并显示一些元素。

我想知道这是正确的做法吗?或者如何改进

$(document).ready(function() {
if ($(".delivery_type:radio").length > 0) {
    if ($('#methodPickup').is(':checked')) {
        $(".methodDelivery").hide();
        $("#addressBookSelectBlock").hide();
        $(".customAddress").hide();
    }
    if ($('#methodDelivery').is(':checked')) {
        $(".methodPickup").hide();
    }
}


$(".delivery_type:radio").live('click', function() {
    if ($(this).val() == "pickup") {
        $(".methodDelivery").hide();
        $(".methodPickup").show();
        $("#addressBookSelectBlock").hide();
        $(".customAddress").hide();
    }
    if ($(this).val() == "delivery") {
        if ($(".selectAddressList").length == 0) {
            $(".customAddress").show();
        }

        $(".methodDelivery").show();
        $(".methodPickup").hide();
        $("#addressBookSelectBlock").show();
    }
});

});

1 个答案:

答案 0 :(得分:1)

您可以将所有hide()show()功能组合在一起:

if ($('#methodPickup').is(':checked')) {
  $(".methodDelivery, #addressBookSelectBlock, .customAddress").hide();
}
// etc...

另外,我不确定为什么要使用live(),除非动态添加或删除单选按钮;如果它们不是动态的,请使用click()

分享一些HTML和更多信息可能有助于提供更多建议。