如何检查所有值并显示一些消息

时间:2014-05-15 17:03:30

标签: javascript jquery input

我做了这个'游戏',现在我想显示一些消息(赢家)什么时候输入的所有值都等于零,我想在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

只有两个零,所以用户是输家。

3 个答案:

答案 0 :(得分:0)

工作demo

尝试将此添加到文档就绪功能的结束

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);

答案 1 :(得分:0)

这样的事情:

var inputs = $('input'), sum;
$('button').on('click', function(){
  sum=0;
  inputs.each(function(){
  sum += +$(this).val();
 });
if(sum > 0){
  console.log('not all zeros');
 }else{
  console.log('all zeros');
}
});

<强> DEMO

答案 2 :(得分:0)

<强> DEMO

$('.inputi').click(function() {
   var counter = 0; 
   $(".inputi").each(function() { 
     if(this.value==0){
         counter= counter+1;
       }
   });

   if(counter==0) {
     // show message here
     alert("You won!");
   } 
   if(counter < 3 & counter >0) {
    //show message when 3 field values are less than 0.
    alert("Sorry, you lost the game");
   }
});