我有一个在我的网站上重复的功能
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')"
但它不起作用。我在这个脚本中的错误在哪里。