javascript jQuery 如果值为空,则将其设置为 0 并进行验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20275415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery if value is empty set it to 0 and validate
提问by Dawid van der Hoven
Simple Question I just cant solve, I have the following function:
我无法解决的简单问题,我有以下功能:
function finalCheck(){
var result = true;
jQuery("#carloan-form :input[type=text]").each(function() {
if(!this.value){this.value = 0;}
if(jQuery.isNumeric(this.value) && this.value>=0) {
jQuery(this).parent().children('.cal-error').slideUp(300)
}else{
result = false;
jQuery(this).parent().children('.cal-error').slideDown(300);
jQuery('.cal-result').slideUp(300);
}
});
return result;
}
basically the problem is if(!this.value){this.value = 0;}
if there is no value i need to set it to zero before carrying on with the validating the form, but this only seems to take effect the second time i validate it.
基本上问题是if(!this.value){this.value = 0;}
如果没有值我需要在继续验证表单之前将其设置为零,但这似乎只在我第二次验证它时生效。
So it all boils down to if i set an inputs value to 0 in the each loop, it only takes it into account after the function is done.
所以这一切都归结为如果我在每个循环中将输入值设置为 0,它只在函数完成后才考虑它。
Here is a JSFIDDLE, Identical to my current problem, only on the second click will the proper value appear.
这是一个JSFIDDLE,与我当前的问题相同,只有在第二次点击时才会出现正确的值。
Any Help Greatly Appreciated.
非常感谢任何帮助。
回答by odiseo
You can set a default value for your print out
您可以为打印输出设置默认值
var finalValue = $('#test').val() || 0;
回答by Snixtor
The issue is when you are gettingthe value, though this isn't evident in your code snippet, only the JSFiddle. So where you've got this:
问题是当您获得值时,虽然这在您的代码片段中并不明显,但只有 JSFiddle。所以你在哪里得到这个:
var finalValue = $('#test').val();
var testInput = test();
if(testInput){
$('span').text(finalValue);
}
It instead needs to be this:
相反,它需要是这样的:
var testInput = test();
if(testInput){
var finalValue = $('#test').val();
$('span').text(finalValue);
}
Note the movement of setting finalValue
to be afterthe call to test()
.
注意设置的运动finalValue
是后调用test()
。
Working fiddle - http://jsfiddle.net/bQSA3/4/
工作小提琴 - http://jsfiddle.net/bQSA3/4/
Perhaps where you were mistaken was thinking the call to $('#test').val()
set a referenceto the value of the test
input. It instead gets a copyof the value. If the value in test
changes, you need to fetch the value again.
也许你错的地方是认为调用$('#test').val()
设置对输入值的引用test
。相反,它会获取该值的副本。如果中的值test
发生变化,则需要再次获取该值。
回答by Sajad Deyargaroo
The below code (simplified) always displays truein alert, which I think is the expected behavior.
下面的代码(简化)总是在alert 中显示true,我认为这是预期的行为。
Html
html
<form id="carloan-form">
<input type="text" />
<input type="text" />
<input type="text" />
<button id="check">check</button>
</form>
Script
脚本
$(function () {
$("#check").click(function() {
var result = true;
jQuery("#carloan-form :input[type=text]").each(function() {
if(!this.value){this.value = 0;}
if(jQuery.isNumeric(this.value) && this.value>=0) {
}else{
result = false;
}
});
alert(result);
});
});
回答by Xoundboy
Try putting this...
试试把这个...
if(!this.value){this.val(0);}