- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个表单,并且希望仅当输入值为数字时才执行代码。我试图避免使用某种验证插件,并且想知道是否有一种方法可以循环输入字段并检查值。
我一直在尝试以下方法,但我认为我的逻辑是错误的:
(#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/
所以我试图获取我单击的 li 的编号并显示相应的页面文本,同时隐藏其他页面文本。 (标记为#text1、#text2、#text3 等...),但下面的代码对我没有任何作用。 非常感谢您的帮助! va
我正在尝试根据数据属性从多个元素中提取所有 ID。 这是我的 html: John Peter 和 JavaScript: var result = []; $('[dat
我有一些代码需要调整。我需要将 .play“rel”替换为 .audio_player“id”。将需要遍历多个“媒体”div。 这是代码 Lis
jQuery 的each() 函数是非阻塞的吗? 在下面的示例中,我希望首先记录“test in every”,但在每个测试中,首先记录“test out of every”。 因此,这导致循环在返回
我正在尝试使用以下each()函数扫描DOM中的所有样式表 $("link").each(function(){ $.get($(this).attr('href'), function()
从 $.each() 中访问 this.rules 变量的最佳方式是什么?任何关于原因/方式的解释也会有帮助! app.Style = function(node) { this.style
基本上我正在做的是制作一种邀请系统,用户单击用户,他们进入一个列表,一切正常,我可以使用each()获取他们的id,但我需要将其传递jQuery Ajax 到 php 将其发送到数据库以获取通知。这基
我的页面上有几个字段都具有相同的类“EntryField”。 在这些字段(任何)内按下按键时,我需要遍历具有 EntryField 类的所有字段。 我可以在 .keyup 上组合 .each 还是需要
使用JQUERY.each()时,如何获取匹配项的值。我想在each() 内包装一个IF 以仅对最后一场比赛起作用。谢谢 最佳答案 这看起来很浪费。相反,只需选择 last项并对其采取行动。例如: $
我正在使用 $().each() 循环访问一些项目。我想确保这段脚本之后的操作仅在 each() 完成时执行。 示例: $('something').each(function() { // do
我正在尝试构建一系列嵌套的计时循环。内部循环迭代具有相同 CSS 类的 6 个项目,并交换图像 4 秒。外循环导致内循环不断重复。因此,image1 交换、image2 交换... image6 交换
我正在运行一个 jQuery 函数,该函数应该根据视频的宽度修改视频的宽度。 我的页面中有很多视频,我想以某种方式使它们适应视频容器,该容器具有固定的宽度。所以在我之前讨论的函数中,我检查视频宽度是否
我为一些 div 编写了一个插件。 textbox 是 div 的类,deactivateTextBox() 是插件。 我需要停用页面上的所有文本框。下面两段代码是否都是这样做的?第二种效率更高吗?
我有 2 个列表。 第一个列表包含所有数据。另一种是占位符。我有一个按钮可以采用默认值。它将第一个列表克隆到占位符中。 现在我使用此代码来提醒所有找到的data-id。效果很好。 但是当我尝试将这些数
我在这里做错了什么吗?我有一个标签数组,当我执行 jQuery each() 时在数组上它不会进入 each()我确实有一个alert但什么也没发生。我检查了错误日志控制台,没有错误。那么,我做错了什
我正在使用 jQuery ColorPicker 小部件 - 特别是行使 ColorPickerSetColor 函数(只是内部的“setColor”)。代码摘录: setColor:
我正在制作一个表单,并且希望仅当输入值为数字时才执行代码。我试图避免使用某种验证插件,并且想知道是否有一种方法可以循环输入字段并检查值。 我一直在尝试以下方法,但我认为我的逻辑是错误的: (#mont
我在类名 fooContainer 的 div 中有一堆类名 foo 的 div(这些 foo div 已动态添加到 fooContainer)。 我有一个函数 operaOnFoo(fooObjec
我正在尝试读取 2 个 CSV 文件,每个文件都包含数字行。 文件1内容: 111 222 文件2内容: 333 444 读取内容后,我将数字推送到新数组 (csv_arr) 并计算出现次数 (c
我有一个迭代每个选择器的函数。但是,我现在只有一个选择器,所以我想将其设置为单一,但我不知道如何操作。 下面是我的代码。 $.each($('[data-behaviour~=datepicker]'
我是一名优秀的程序员,十分优秀!