【问题标题】:Radio - click action and length action单选 - 点击动作和长度动作
【发布时间】:2011-05-05 12:42:36
【问题描述】:

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

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

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

$(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();
    }
});

});

【问题讨论】:

    标签: javascript jquery jquery-selectors jquery-events


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多