我做了这个“游戏”,现在我想在输入的所有值都等于零时显示一些消息(获胜者),而当3个字段值小于0时我要显示消息。
示例:当字段是这样时(连接链接)
http://prntscr.com/3jgvz6
那么用户就是赢家,但是就像这样(只有两个字段为0)
http://prntscr.com/3jgw6f
那么用户将自动成为失败者。
链接到jsbin:http ://jsbin.com/kezesefu/1
我的代码很长才能放在这里。
当25个字段中只有两个0时,用户将输掉比赛。
2 3 1 2 0 2 2 1 1 1 3 0 3 1 2 2 3 4 1 2
只有两个零,所以用户是失败者。
工作演示
尝试将其添加到文档准备功能的末尾:
function verifyWinningConditions () {
var nonZeroBoxes = 25;
$('input').each(function(){
if($(this).val() == 0) {
nonZeroBoxes--;
}
});
if (nonZeroBoxes == 0) {
alert('WINNER!! All 25 boxes are 0!');
}
if(nonZeroBoxes >= 23) {
alert('LOSER! Only 2 out of 25 boxers were 0!');
}
}
$('input').on('click', verifyWinningConditions);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句