【发布时间】:2014-02-18 15:05:05
【问题描述】:
我有多个控件,不同类型的控件具有相同的行为。 如何使用单个函数来重用相同的行为。现在我对他们每个人都有一个功能。这个函数可以正常工作,但我想找到一种方法,如果我们可以用一个全局函数替换这个函数并重用它。
$(document).ready(function () {
var threechecked = false;
var fourchecked = false;
$('#<%=CheckBoxList1.ClientID%> input:checkbox').click(function () {
var currentIdone = 'Unknown';
var currentId = $(this).next().html();
if (currentId == currentIdone) {
if (threechecked) {
$("#<%=CheckBoxList1.ClientID%> input").removeAttr('disabled');
threechecked = false;
return;
}
else {
$("#<%=CheckBoxList1.ClientID%> input").attr('checked', false);
$(this).attr('checked', true);
$('#<%=CheckBoxList1.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
threechecked = true;
}
}
});
$('#<%=CheckBoxList2.ClientID%> input:checkbox').click(function () {
var currentIdone = 'Unknown';
var currentId = $(this).next().html();
if (currentId == currentIdone) {
if (fourchecked) {
$("#<%=CheckBoxList2.ClientID%> input").removeAttr('disabled');
checked = false;
return;
}
else {
$("#<%=CheckBoxList2.ClientID%> input").attr('checked', false);
$(this).attr('checked', true);
$('#<%=CheckBoxList2.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
fourchecked = true;
}
}
});
有没有一种方法可以创建单个函数并由每个控件重用相同的函数?
【问题讨论】:
-
为什么不把它做成一个jQuery插件呢?
标签: jquery jquery-ui validation checkboxlist