1
我有哪一個是重複遍佈在我的網站如何格式化正常jQuery代碼可重用的代碼
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(此, 'ownerDay', 'ownerDaytype', 'ownerActive')」
但它不工作。我的錯誤在哪裏是這個腳本。