您好我正在尝试从html中的文本框中获取值并将其提供给for循环中的i,但我的程序不起作用:
var k = document.getElementById(" textbox1")。value;
for (var i = k; i < n; i+=4) {
//for (var j = 0; j < n; j+=4) {
pixelComponents[i ] = pixelComponents[i ] + 100; //red
pixelComponents[i+1] = pixelComponents[i+1]+0; //green
pixelComponents[i+2] = pixelComponents[i+2]+0; //blue
//}
}
我也尝试了这个,它也不起作用:
for (var i = document.getElementById("textbox1").value;; i < n; i+=4) {
//for (var j = 0; j < n; j+=4) {
pixelComponents[i ] = pixelComponents[i ] + 100; //red
pixelComponents[i+1] = pixelComponents[i+1]+0; //green
pixelComponents[i+2] = pixelComponents[i+2]+0; //blue
//}
}
答案 0 :(得分:2)
文本框的值是一个字符串。尝试做:
var k = parseInt(document.getElementById("textbox1").value, 10);
有趣的是,在javascript中对字符串执行++会将其转换为数字,但是执行+ = 4会将字符4添加到其中:
var x = "1"; console.log(++x);
2
var x = "1"; console.log((x += 4));
14
请注意,即使您将文本框的值设置为数字,当您检索它时,它又是一个字符串:
var textBox = document.getElementById("someTextBox");
textBox.value = 8;
alert(textBox.value + 4); // 84, not 12