当我按下按钮时,它会从textarea中检索一个值 HTML代码
<textarea rows="10" cols="17" id="list_card" placeholder="Insert Card Numbers"></textarea>
<input name="infoDominio" type="submit" class="btn btn-primary btn-block btn-sm" value="Add Card" onclick="add_card()">
Javascript代码
var res = myVar[0];
var ress = 4567891;
var val1 = Number.isInteger(res);
var val2 = Number.isInteger(ress);
document.write(myVar[0]+" "+val1+" "+val2+" "+res);
myVar[0] = 4567891; myVar[0]
值来自textarea。但我无法获得正确的输出。我认为val1应该是真的。输出看起来像这样。你能告诉我怎样才能得到val1 = true。
4567891 false true 4567891
答案 0 :(得分:1)
textarea
将生成string
而不是integer
。
您需要parse为integer
:
var res = parseInt(myVar[0]);
作为旁注:用户可以在textarea中键入非整数值。也许您正在寻找input type="number"/>
?
答案 1 :(得分:1)
我觉得这对你很有用
function add_card(){
myVar = document.getElementById("list_card").value;
var res = Number(myVar);
var ress = 4567891;
var val1 = Number.isInteger(res);
var val2 = Number.isInteger(ress);
document.write(myVar+" "+val1+" "+val2+" "+res);
}
&#13;
<textarea rows="10" cols="17" id="list_card" placeholder="Insert Card Numbers"></textarea>
<input name="infoDominio" type="submit" class="btn btn-primary btn-block btn-sm" value="Add Card" onclick="add_card()">
&#13;
答案 2 :(得分:0)
只需将val1赋值更改为此
即可var val1 = Number.isInteger(parseInt(res));
答案 3 :(得分:0)
使用此正则表达式
function add_card(){
var res = document.getElementById('list_card').value;
var ress = 4567891;
var val1 = /^\d+$/.test(res);
var val2 = /^\d+$/.test(ress);
document.write(res+" "+val1+" "+val2+" "+ress);
}
<textarea rows="10" cols="17" id="list_card" placeholder="Insert Card Numbers"></textarea>
<input name="infoDominio" type="submit" class="btn btn-primary btn-block btn-sm" value="Add Card" onclick="add_card()">