如何通过ID获取多个输入字段的值
因此,我的表格最终具有4个块结果,结果基于用户选择的答案。我想做的是单独获取所有块的值(我尝试通过DOM执行此操作,但由于某种原因,我观察到值属性为空白),并比较所有这些值以找到最大的值,我将使用它对于要在结果页面上实现的逻辑。
我尝试使用以下代码来实现这一目标,但是有一些迭代正在进行中,我不确定为什么。我已经宣布了一个值的数组,并将其登录到控制台以澄清。
我仍然没有精通JavaScript,所以请原谅我。请指导我重定向。
// get values of the CAT scores from the DOM & compare to get largest value
$('#field_wyepk, #field_wyepk2, #field_wyepk3 , #field_wyepk32').change(function(){
var commerceScore = $("#field_wyepk").val();
var scitechScore = $("#field_wyepk2").val();
var humanitiesScore = $("#field_wyepk3").val();
var eduScore = $("#field_wyepk32").val();
console.log(commerceScore);
console.log(scitechScore);
console.log(humanitiesScore);
console.log(eduScore);
var array = [commerceScore,scitechScore, humanitiesScore,eduScore];
console.log(array);
var largest= 0;
for (i=0; i<=largest; i++){
if (array[i]>largest && array[i] !== ""){
var largest=array[i];
}
}
});
so I have a form which has 4 blocks of results at the end, the results are based on answers selected by the user. What I want to do is get the values of all the blocks individually( I have tried doing this via the DOM but I observed the value attribute is blank for some reason) and compare all of them to find the largest value, which I will use for some logic that I want to implement on the results page.
I have tried using the code below to achieve that, however there is some iteration going on and I am not sure why.I have declared an array for the values and logged it to the console for clarity.
I am still not proficient in JavaScript, so please pardon me.Please kindly guide me in the redirection.
// get values of the CAT scores from the DOM & compare to get largest value
$('#field_wyepk, #field_wyepk2, #field_wyepk3 , #field_wyepk32').change(function(){
var commerceScore = $("#field_wyepk").val();
var scitechScore = $("#field_wyepk2").val();
var humanitiesScore = $("#field_wyepk3").val();
var eduScore = $("#field_wyepk32").val();
console.log(commerceScore);
console.log(scitechScore);
console.log(humanitiesScore);
console.log(eduScore);
var array = [commerceScore,scitechScore, humanitiesScore,eduScore];
console.log(array);
var largest= 0;
for (i=0; i<=largest; i++){
if (array[i]>largest && array[i] !== ""){
var largest=array[i];
}
}
});
Please see attached snippet of the console results
All I need is to get an array with [71, 85, 76, 56] as an example using the snippet and I will do the rest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中所说,您有一个更改听众倾听所有结果的更改。每当更改结果之一时,将所有结果都添加到您的数组中并将其登录到控制台。
也许更改的侦听器不是您需要的 - 也许您可以在输入所有结果时让用户单击一个按钮,然后在按钮上单击创建数组。
否则,您可以解析数组并检查所有值是否具有长度?
(如果您的数组包含一个或多个空字符串的实例,则会返回false,如果您的数组有效。仅在值为True(如果值为true)时,请继续进行下一步)。
还请注意,您的数组是由字符串而不是整数组成的。我不确定这是否是您想要的。
As was said in the comment, you have a change listener listening for changes to all of the results. Any time one of the results is changed, all of them are added to your array and logged to the console.
Maybe a change listener isn't what you need - perhaps you could get the user to click a button when all the results have been entered, then on the button click create the array.
Otherwise, you could parse your array and check that all the values have a length?
(will return false if your array contains one or more instances of empty string, or true if your array is valid. Only proceed to the next step if the value is true)
Also note that your array is made up of strings, not integers. I'm not sure if this is what you want.