【发布时间】:2018-02-08 13:35:07
【问题描述】:
我需要一个要修改的字符串,只要检查/取消选中任何一个复选框,只有当其他人丢弃了一个例外错误时,只有第一个复选框就是作业: " "#two-stars".is 不是函数"。我尝试了不同的条件,但这段代码的核心似乎有问题,我还没有找到。代码:
$('#input:checkbox').change(function(){
starsQueries = '';
if( $('#two-stars').is(':checked') ){
starsQueries += '&stars=401';
console.log('checked!');
}
if( $('#three-stars').is(':checked') ){
if( ('#two-stars').is(':checked') ){
starsQueries += ',402';
} else {
starsQueries += '&stars=402';
}
}
if( $('#four-stars').is(':checked') ){
if( ('#three-stars').is(':checked') || ('#two-stars').is(':checked') ){
starsQueries += ',403';
} else {
starsQueries += '&stars=403';
}
}
if( $('#five-stars').is(':checked') ){
if( ('#four-stars').is(':checked') || ('#three-stars').is(':checked') || ('#two-stars').is(':checked') ){
starsQueries += ',404';
} else {
starsQueries += '&stars=404';
}
}
});
在 JSfiddle 上:https://jsfiddle.net/4mLcz6mt/1/
【问题讨论】:
-
代码似乎过于复杂,但如果您仔细查看代码,您会发现复制/粘贴错误。
-
那里缺少一些 jquery(即
$) -
让你的生活更容易维护,摆脱所有那些如果jsfiddle.net/vf9L1cko
标签: javascript jquery html checkbox