这里是我努力奋斗的代码。我想将两个输入连在一起,并将结果保存为整数(JS中的数字' s)。
var secsVal = -1;
function valueAssign(i) {
if (secsVal == -1){
document.getElementById("countdown").value = i;
document.getElementById("countdown").innerHTML = (i);
secsVal = i;
}
else {
secsVal = "" + secsVal + i;//concatenating first value of i to the second.
secsVal = secsVal.map(Number);//trying to convert back to num, but I think map() needs to see an array, which I don't think I got here.
document.getElementById("countdown").value = secsVal;
document.getElementById("countdown").innerHTML = (secsVal);//I want to replace the first displayed digit here, with the new (concatenated) number.
}
}
答案 0 :(得分:1)
试试这个
secsVal = +("" + secsVal + i);
答案 1 :(得分:1)
在输入标记中使用数字作为值是没有意义的。类型总是一个字符串。
要转换为数字,请使用Number
或一元+
secsVal = Number(secsVal);
或
secsVal = +secsVal;
答案 2 :(得分:0)
secsVal = Number('' + secsVal + i) // explicit cast to number
secsVal = +('' + secsVal + i) // implicit cast to number
secsVal = parseInt('' + secsVal + i) // explicit cast to integer
secsVal = ~~('' + secsVal + i) // implicit cast to integer
答案 3 :(得分:0)
只需使用+secsVal
var secsVal = -1;
function valueAssign(i) {
if (secsVal == -1){
document.getElementById("countdown").value = i;
document.getElementById("countdown").innerHTML = (i);
secsVal = i;
}
else {
secsVal = "" + secsVal + i;
console.log(typeof secsVal);//secsVal is a string
secsVal = +secsVal;
console.log(typeof secsVal); //secsVal is now a number
document.getElementById("countdown").value = secsVal;
}
}

<input type="number" id="countdown"/>
<button onclick="valueAssign(5)">Click</button>
&#13;
答案 4 :(得分:0)
如何解析String?
“parseInt()函数解析一个字符串并返回一个整数。” http://www.w3schools.com/jsref/jsref_parseint.asp