Javascript 如何检查所有值并显示一些消息
我做了这个“游戏”,现在我想在输入的所有值都等于零时显示一些消息(赢家),并且我想在3个字段值小于0时显示消息 示例: 当字段如下所示时(连接链接) 然后用户就是赢家,但当它是这样的时候(只有两个字段和0) 那个么用户就自动地失败了 链接到jsbin: 我的代码太长了,不能放在这里 当25个字段中只有两个0时,用户将输掉游戏。前 2 3 1 2 0 2 2 1 1 1 3 0 3 1 2 2 3 4 1 2 2 3 1 2 0 2 2 1 1 1 3 0 3 1 2 2 3 4 1 2 只有两个零,所以用户是失败者。正在工作 尝试将其添加到文档准备功能的末尾:Javascript 如何检查所有值并显示一些消息,javascript,jquery,input,Javascript,Jquery,Input,我做了这个“游戏”,现在我想在输入的所有值都等于零时显示一些消息(赢家),并且我想在3个字段值小于0时显示消息 示例: 当字段如下所示时(连接链接) 然后用户就是赢家,但当它是这样的时候(只有两个字段和0) 那个么用户就自动地失败了 链接到jsbin: 我的代码太长了,不能放在这里 当25个字段中只有两个0时,用户将输掉游戏。前 2 3 1 2 0 2 2 1 1 1 3 0 3 1 2 2 3 4 1 2 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);
大概是这样的:
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');
}
});
$('.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");
}
});
$('.inputi')。单击(函数(){
var计数器=0;
$(“.inputi”).each(function(){
if(this.value==0){
计数器=计数器+1;
}
});
如果(计数器==0){
//在此处显示消息
警惕(“你赢了!”);
}
如果(计数器<3&计数器>0){
//当3个字段值小于0时显示消息。
警惕(“对不起,你输了比赛”);
}
});
请在您的问题中发布您的代码。我不理解失败场景的标准。您好,欢迎来到StackOverflow!请在您的问题中添加一些代码和详细信息,而不仅仅是链接。我们不喜欢只关注任何随机链接。@未定义:OP似乎在寻求关于添加其他实现的帮助,因此这在代码审阅中是离题的。@Jamal我的意思是“我建议在解决当前问题后在codereview.stackexchange.com上发布一个问题”。move counter=0,在点击事件中,我的答案中的链接也被更新了。希望它满足您的条件:)我不能尝试使用全零,但如果使用少于3,它就可以工作。谢谢你:我不能投票,因为我的名声在15岁以下如果(计数器<3&计数器>0){并且它将在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');
}
});
$('.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");
}
});