我有个问题。jQuery('#FinishTest').click()可以工作,但是当您选择所有答案时,它就会停止。控制台中没有出现错误。

我的代码
jQuery('#FinishTest').click(function() {
var names = {};
jQuery(':radio').each(function() {
names[jQuery(this).attr('name')] = true;
});
var count = 0;
jQuery.each(names, function() {
count++;
});
if (jQuery(':radio:checked').length > 3) {
if (jQuery(':radio:checked').length < count) {
if (confirm("Henüz cevaplanmamış sorularınız var. Testi bitirmek istediğinizden eminmisiniz ?")) {
finishtest();
} else {
jQuery('#FinishTest').bind('click');
return;
}
}
} else {
alert('Testi bitirebilmek için en az 2 soru cevaplamalısınız.');
}
});发布于 2017-11-14 17:14:22
这是因为您的代码中存在以下条件:
if( jQuery(':radio:checked').length < count )当您选择所有答案时,您将以jQuery(':radio:checked').length =6 6结束,count变量值也将是6,因此条件永远不会计算为true。
尝试向您的条件=添加相等的条件,以便它将是次等的或相等的<=:
if( jQuery(':radio:checked').length <= count ){
//Your logic here
}希望这能有所帮助。
改变并解决了问题。
if (jQuery(':radio:checked').length > 3) {
if (jQuery(':radio:checked').length < count) {
if (confirm("Henüz cevaplanmamış sorularınız var. Testi bitirmek istediğinizden eminmisiniz ?")) {
finishtest();
}
} else {
finishtest();
}
} else {
alert('Testi bitirebilmek için en az 2 soru cevaplamalısınız.');
}
发布于 2017-11-14 17:18:20
代码中没有错误。在if语句中,只有条件是错误的。我已经用调试在浏览器(Chrome)上进行了测试。在这里输入图像描述
我希望它能帮助你。
https://stackoverflow.com/questions/47291420
复制相似问题