简体   繁体   English

javascript函数检索input [type = number]并根据值过滤它们

[英]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>&nbsp;<input id="fedgvt" min="0" name="fedgvt" size="4" type="number" value="0"></div>

<div class="fieldEntry"><label for="gvt">state or local</label>&nbsp;<input id="stategvt" min="0" name="stategvt" size="4" type="number" value="0"></div>

<div class="fieldEntry"><label for="tribalgvt">tribal</label>&nbsp;<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.

相关问题 使用javascript过滤输入类型编号 - Filter Input type number using javascript 如何基于javascript中的输入值调用函数? - how to call a function based on the input value in javascript? 带有可变数量参数的Javascript函数-更改其值-然后返回 - Javascript function that takes variable number of arguments - changes their value - and then returns them 如何使用react和javascript根据输入字段中的值过滤数据? - How to filter data based on value in input field using react and javascript? 使用纯Javascript创建输入类型=无线电并按名称检索它们(即ie9上的7个标准) - Create input type=radio with pure Javascript and retrieve them by name (ie7 standards on ie9) 根据用户在Javascript中的输入获取输入表单的值和id的函数 - a function to get value and id of an input form based on user input in Javascript 如何检索函数输入的javascript - How to retrieve function input javascript 如何使用javascript根据输入值突出显示数字的相关范围? - How to highlight relevant range of number based on input value using javascript? 如何从输入类型编号中取值? 的JavaScript - How to take value from input type number ? JavaScript JavaScript 递增和递减输入类型中的 BigInt 值 number max - JavaScript increment and decrement BigInt value in input type number max
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM