gpt4 book ai didi

jquery - 使用 Jqueryeach() 循环输入字段进行验证

转载 作者:行者123 更新时间:2023-12-03 21:39:01 24 4
gpt4 key购买 nike

我正在制作一个表单,并且希望仅当输入值为数字时才执行代码。我试图避免使用某种验证插件,并且想知道是否有一种方法可以循环输入字段并检查值。

我一直在尝试以下方法,但我认为我的逻辑是错误的:

(#monthlyvenue 是表单 ID)

$("#submit").click(function() {

$("#monthlyincome input").each(function() {

if (!isNaN(this.value)) {
// process stuff here

}

});

});

有什么想法吗?

这是完整更新的代码:

$("#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 是一个用于...格式化数字的函数。 :)

最佳答案

在这里进行测试,效果很好:

$(function() {
$("#submit").click(function() {
$("#myForm input[type=text]").each(function() {
if(!isNaN(this.value)) {
alert(this.value + " is a valid number");
}
});
return false;
});
});

在如下所示的表单上:

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

根据需要移动 return false

编辑:链接到添加到 OP 表单的代码 http://pastebin.com/UajaEc2e

关于jquery - 使用 Jqueryeach() 循环输入字段进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4758259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com