循环遍历输入字段以使用Jquery each() [英] Looping through input fields for validation using Jquery each()
问题描述
我正在制作一个表单,并希望仅当输入值是数字时才执行代码。我试图避免使用某种验证插件,并想知道是否有办法循环输入字段并检查值。
我一直在尝试以下方法,但我认为我的逻辑错误:
(#monthlyincome是表单id)
$(#submit)。click(function(){
$ (#monthlyincome input)。each(function(){
if(!isNaN(this.value)){
//在这里处理的东西
}
});
});
有什么想法?
这是整个更新的代码:
$(#submit)。click(function(){
$(#monthlyincome input [type = text])。each(function(){
if(!isNaN(this.value)){
//处理数据
var age = parseInt($(#age).val());
var startingage = parseInt($(#startingage).val());
if(startingage - age > 0){
$(#field1).val(startingage - age);
$(#field3).val($(#field1)。 val());
var inflation = $(#field4)。 100;
var inflationfactor = Math.pow(1 + inflationprc,inflationyrs);
$(#field5).val(inflationfactor.toFixed(2));
var estyearlyinc = $(#field6)。val();
var inflatedyearlyinc = inflationfactor * estyearlyinc;
$(#field7)。val(FormatNumberBy3(inflatedyearlyinc.toFixed(0),,,。));
var estincyears = $(#field2)。val();
var esttotalinc = estincyears * inflatedyearlyinc;
$(#field8)。val(FormatNumberBy3(esttotalinc.toFixed(0),,,。));
var investmentrate = $(#field9)。val()/ 100;
var investmentfactor = Math.pow(1 +投资率,通货膨胀率);
$(#field10)。val(investmentfactor.toFixed(2));
var currentsavings = $(#field11)。val();
var futuresavings = currentavings * investmentfactor;
$(#field12)。val(FormatNumberBy3(futuresavings.toFixed(0),,,。));
//最终计算
var futurevalue =(1 *(Math.pow(1 +投资率,通货膨胀率)-1)/投资率);
var finalvalue =(1 / futurevalue *(esttotalinc - futuresavings));
$(#field13)。val(FormatNumberBy3(finalvalue.toFixed(0),,,。));
}
//结束处理
}
});
});
FormatNumberBy3是格式化数字的函数。 :)
<$ p $(#submit)。click(function(){
$(#myForm input [type = text]) .each(function(){
if(!isNaN(this.value)){
alert(this.value +是一个有效的数字);
}
} );
返回false;
});
});
放在一张表格上:
< form method =postaction =id =myForm>
< input type =textvalue =1234/>
< input type =textvalue =1234fd/>
< input type =textvalue =1234as/>
< input type =textvalue =1234gf/>
< input type =submitvalue =Sendid =submit/>
< / form>
在您认为合适的情况下将返回值设为false
编辑:链接到代码sdded到OP
http://pastebin.com/UajaEc2e
I'm making a form and would like the code to execute only if the input values are numbers. I'm trying to avoid using some kind of validation plugin and was wondering if there is a way to loop through the input fields and check for the values.
I've been trying the following but I think my logic is wrong:
(#monthlyincome is the form id)
$("#submit").click(function() {
$("#monthlyincome input").each(function() {
if (!isNaN(this.value)) {
// process stuff here
}
});
});
Any ideas?
This is the whole updated code:
$("#submit").click(function() {
$("#monthlyincome input[type=text]").each(function() {
if (!isNaN(this.value)) {
// processing data
var age = parseInt($("#age").val());
var startingage = parseInt($("#startingage").val());
if (startingage - age > 0) {
$("#field1").val(startingage - age);
$("#field3").val($("#field1").val());
var inflationyrs = parseInt($("#field3").val());
var inflationprc = $("#field4").val() / 100;
var inflationfactor = Math.pow(1 + inflationprc, inflationyrs);
$("#field5").val(inflationfactor.toFixed(2));
var estyearlyinc = $("#field6").val();
var inflatedyearlyinc = inflationfactor * estyearlyinc;
$("#field7").val(FormatNumberBy3(inflatedyearlyinc.toFixed(0), ",", "."));
var estincyears = $("#field2").val();
var esttotalinc = estincyears * inflatedyearlyinc;
$("#field8").val(FormatNumberBy3(esttotalinc.toFixed(0), ",", "."));
var investmentrate = $("#field9").val() / 100;
var investmentfactor = Math.pow(1 + investmentrate, inflationyrs);
$("#field10").val(investmentfactor.toFixed(2));
var currentsavings = $("#field11").val();
var futuresavings = currentsavings * investmentfactor;
$("#field12").val(FormatNumberBy3(futuresavings.toFixed(0), ",", "."));
//final calculations
var futurevalue = (1 * (Math.pow(1 + investmentrate, inflationyrs) - 1) / investmentrate);
var finalvalue = (1 / futurevalue * (esttotalinc - futuresavings));
$("#field13").val(FormatNumberBy3(finalvalue.toFixed(0), ",", "."));
}
// end processing
}
});
});
FormatNumberBy3 is a function for... formatting the numbers. :)
Well testing here this works just fine:
$(function() {
$("#submit").click(function() {
$("#myForm input[type=text]").each(function() {
if(!isNaN(this.value)) {
alert(this.value + " is a valid number");
}
});
return false;
});
});
on a form looking like this:
<form method="post" action="" id="myForm">
<input type="text" value="1234" />
<input type="text" value="1234fd" />
<input type="text" value="1234as" />
<input type="text" value="1234gf" />
<input type="submit" value="Send" id="submit" />
</form>
Move the return false around as you see fit
Edit: link to code sdded to OPs form http://pastebin.com/UajaEc2e
这篇关于循环遍历输入字段以使用Jquery each()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!