我有多个控件,不同类型的控件具有相同的行为。如何使用单个函数重用相同的行为。现在,我为他们每个人都有一个功能。这个函数工作正常,但是我想找到一种方法,如果我们可以用一个全局函数替换这个函数并重用它。
$(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;
}
}
});是否有一种方法可以通过每个控件创建单个函数并重用相同的函数?
发布于 2014-02-19 14:54:29
我想是这样的
$( ".unknownSelectable input" ).change(function( event ){
var labelSelected = $( this ).next().html();
if ( labelSelected == "Unknown" ) {
var $siblings = $( this ).siblings( "input[type='checkbox']" );
if( $( this ).is( ":checked" ) )
{
$siblings.prop( "checked", false );
$siblings.attr( "disabled", "disabled" );
}
else{
$siblings.removeAttr( "disabled" );
}
}
});JSBin
https://stackoverflow.com/questions/21857625
复制相似问题