[英]javascript function retrieve input[type=number] and filter them based on the value
I have several input fields of type number, which users have to fill in. I need a script that will display a new page only once users have at least one non-zero value for these fields. 我有几个数字类型的输入字段,用户必须填写。我需要一个脚本,该脚本仅在用户对这些字段具有至少一个非零值时才显示新页面。 So I need a function that checks that at least one input of type number is larger than zero.
因此,我需要一个函数来检查至少一个类型编号输入是否大于零。 Below is the function, in the script, that I wrote to attempt to implement this conditional page-switching (it calls another function which works and I am not showing here).
下面是我在脚本中尝试实现该条件页面切换的函数(它调用了另一个有效的函数,此处未显示)。 The issue is with var filledValue.
问题在于var filledValue。 It returns undefined.
返回未定义。 I would like it to get all input elements of type number that have a raw value larger than 0 and return the id of that element.
我希望它获取原始值大于0的所有number类型的输入元素,并返回该元素的id。 If the resulting variable is non-empty, then move on.
如果结果变量为非空,则继续。
function NextTrial() {
var filledValue = $("input[type=number].val()>0",
"#page" + order[currentTrial]).id;
if (filledValue) {
$("#page" + order[currentTrial]).hide();
currentTrial++;
ShowTrial(currentTrial);
} else {
alert('Please answer the question before moving on!');
}
}
A sufficient excerpt of the HTML is: HTML的足够摘录是:
<div id="answercol1">
<p><big>Government:</big></p>
<div class="fieldEntry"><label for="gvt">federal (USA)</label> <input id="fedgvt" min="0" name="fedgvt" size="4" type="number" value="0"></div>
<div class="fieldEntry"><label for="gvt">state or local</label> <input id="stategvt" min="0" name="stategvt" size="4" type="number" value="0"></div>
<div class="fieldEntry"><label for="tribalgvt">tribal</label> <input id="tribalgvt" min="0" name="tribalgvt" type="number" value="0"></div>
</div>
The function that I ended up using is: 我最终使用的功能是:
function NextTrial() {
var answers = 0;
$("input[type=number]").each(function(){
if ($(this).val()>0) {
answers++
}
})
if (answers>0){
$("#page" + order[currentTrial]).hide();
currentTrial++;
ShowTrial(currentTrial);
} else {
alert('Please answer the question before moving on!');
}
}
You have an invalid code and it's not clear what you're trying to achieve,I'm not sure if that really what you want but try the following : 您的代码无效,目前尚不清楚要实现的目标,我不确定那是否确实是您想要的目标,但是请尝试以下操作:
function NextTrial() {
//"#page" + order[currentTrial]).id; //Not sure what this line is used for
var currentTrial = 0;
$("input[type=number]").each(function(){
if ($(this).val()>0) {
$("#page" + order[currentTrial]).hide();
currentTrial++;
ShowTrial(currentTrial);
} else {
alert('Please answer the question before moving on!');
}
})
}
That will loop through all the fields with type number and check if the value of everyone is >0
. 这将遍历所有类型为number的字段,并检查所有人的值是否
>0
。
Hope this helps. 希望这可以帮助。
var ids = ['fedgvt', 'stategvt', 'tribalgvt'];
function GetValidInputs(inputIds){
//ids to return
var rets = [];
//loop through given inputs
inputIds.each(function(id){
//get input value of each based on their id
var val = ("#"+id).val() > 0;
//ternary: if value is > 0 push it to array GetValidInputs returns
//else do nothing
val > 0 ? rets.push(val) : 0;
};
//return desired ids
return rets;
};
//call it and see if it works
GetValidInputs(ids);
Something like the above would work if i'm understanding your question. 如果我理解您的问题,上述类似的方法将起作用。 edit: added comments for clarity
编辑:添加注释以求清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.