【问题标题】:How to format a reusable code from normal jquery code如何从普通 jquery 代码格式化可重用代码
【发布时间】:2013-07-31 13:59:21
【问题描述】:

我的网站有一个重复的功能

jQuery

$(function() {

//check the enabled state on load
if(!$(".notifyOwner").is(':checked')){
    $(".ownerDay").attr("disabled", "disabled");
     $(".ownerDaytype").attr("disabled", "disabled");
}

//toggle the enabled state when the checkbox is clicked
$(".notifyOwner").click(function() {

    if($(this).is(":checked")) {
   $(".ownerDay").removeAttr("disabled");
   $(".ownerDaytype").removeAttr("disabled");
   $(".ownerActive").removeClass("disabled");
} else {
     $(".ownerDay").attr("disabled", "disabled");
   $(".ownerDaytype").attr("disabled", "disabled");
    $(".ownerActive").addClass("disabled");
}



});

});

我尝试将其作为可重用代码,如下所示

function selectToggle(obj, dayclass, daytypeclass, txtactive){
var $event = $(obj);

if(!($event).is(":checked"))
{
    $(dayclass).attr("disabled", "disabled");
    $(daytypeclass).attr("disabled", "disabled");
}

 $event.click(function() {

    if($event.is(":checked"))
    {
     $(dayclass).removeAttr("disabled");
   $(daytypeclass).removeAttr("disabled");
   $(txtactive).removeClass("disabled");
} else {
     $(dayclass).attr("disabled", "disabled");
   $(daytypeclass).attr("disabled", "disabled");
    $(txtactive).addClass("disabled");
}
 });
}

并将其称为 onclick="selectToggle(this,'ownerDay','ownerDaytype','ownerActive')"

但它不起作用。我在这个脚本中的错误在哪里。

【问题讨论】:

    标签: jquery code-reuse reusability


    【解决方案1】:

    您不会将 jquery select 称为类。 IE。在您的通话中,“ownerDay”应该是“.ownerDay”。其他的也一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-30
      • 2016-07-04
      • 2011-04-13
      • 2013-09-09
      • 1970-01-01
      • 2011-04-20
      • 2020-04-09
      • 2011-08-27
      相关资源
      最近更新 更多