admin管理员组文章数量:1405613
I get values and ids from a form input using onkeyup(). If the value is between 0 and 4 I want to add it to an array. However it seems my function adds values to the array regardless if they are between 0 and 4 eventhough I want it to add values to the array only when the value is between 0 and 4. What am I doing wrong?
These are the inputs:
<input type="number" class="form-control" id=0 onkeyup="a(this)" mix="0" max="4">
<input type="number" class="form-control" id=1 onkeyup="a(this)" mix="0" max="4">
.
.
This is my function:
function a(c){
var dps = [];
var id = c.id;
var valueStr = c.value;
var value = parseInt(valueStr)
if (0 <= value <= 4) {
dps[id]=value;
console.log("id: ",id);
console.log("value: ", value);
console.log("length of array: ",dps.length);
console.log("type of value: ", typeof value);
}
}
I have recreated it on Jsfiddle here: /
I get values and ids from a form input using onkeyup(). If the value is between 0 and 4 I want to add it to an array. However it seems my function adds values to the array regardless if they are between 0 and 4 eventhough I want it to add values to the array only when the value is between 0 and 4. What am I doing wrong?
These are the inputs:
<input type="number" class="form-control" id=0 onkeyup="a(this)" mix="0" max="4">
<input type="number" class="form-control" id=1 onkeyup="a(this)" mix="0" max="4">
.
.
This is my function:
function a(c){
var dps = [];
var id = c.id;
var valueStr = c.value;
var value = parseInt(valueStr)
if (0 <= value <= 4) {
dps[id]=value;
console.log("id: ",id);
console.log("value: ", value);
console.log("length of array: ",dps.length);
console.log("type of value: ", typeof value);
}
}
I have recreated it on Jsfiddle here: https://jsfiddle/89az7u4n/
Share asked Feb 15, 2019 at 17:52 MoeMoe 671 gold badge1 silver badge10 bronze badges3 Answers
Reset to default 5The way you typically write inequalities in mathematics is different from the way we typically write parisons in most programming languages.
if (0 <= value <= 4)
doesn't mean 'if the value is between 0 and 4'. The parser treats this as (0 <= value) <= 4
which will either evaluate to false <= 4
or true <= 4
which both evaluate to true
.
Change your if
statement to this:
if (0 <= value && value <= 4) {
...
}
Where &&
is the logical AND operator.
Perhaps you should split your condition into two statements like this:
if (0 <= value && value <= 4) {
instead?
N.B.
- You should avoid using inline on* handlers (onclick, onchange, onkeyup, etc) and use event listeners instead.
- Change
mix="0"
tomin="0"
. - You need to use the
&&
(AND operator) in yourif condition
as shown and explained in @p.s.w.g's answer.
SOLUTION:
If both your inputs are kept next to each other, just wrap them inside a mon div and target them both using the querySelectorAll() method and then use the forEach() method to add a keyup
listener to both the inputs that will run a function checking the input values when the event is invoked.
Check and run the following Code Snippet for a practical example of what I have described above:
/* JavaScript */
const inputVal = document.querySelectorAll("#someDiv input");
let dps = [];
inputVal.forEach(input => input.addEventListener("keyup", function(){
let value = parseInt(input.value);
if (value >= 0 && value <= 4){
dps[input.id] = value;
console.log(dps);
}
}));
<!-- HTML -->
<div id="someDiv">
<input type="number" class="form-control" id="0" min="0" max="4">
<input type="number" class="form-control" id="1" min="0" max="4">
</div>
If your input elements are not kept next to each other, just target them separately using the getElementById() method and then add a keyup
listener to both the inputs that will run a function checking the input values when the event is invoked.
Check and run the following Code Snippet for a practical example of what I have described above:
/* JavaScript */
const inputVal1 = document.getElementById("0");
const inputVal2 = document.getElementById("1");
let dps = [];
function checkVal(){
let value = parseInt(this.value);
if (value >= 0 && value <= 4){
dps[this.id] = value;
console.log(dps);
}
}
inputVal1.addEventListener("keyup", checkVal);
inputVal2.addEventListener("keyup", checkVal);
<!-- HTML -->
<input type="number" class="form-control" id="0" min="0" max="4">
<div>ABCD</div>
<input type="number" class="form-control" id="1" min="0" max="4">
本文标签: htmladding values to an array through an if statement in javascriptStack Overflow
版权声明:本文标题:html - adding values to an array through an if statement in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744909177a2631808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论