通過這種形式從HTML:不能發送形式
<form name="testform" onsubmit="return processForm(this)">
Number 1: <input type="text" name="number1"/><br />
Number 2: <input type="text" name="number2"/><br />
Number 3: <input type="text" name="number3"/><br />
Number 4: <input type="text" name="number4"/><br />
Number 5: <input type="text" name="number5"/><br />
Number 6: <input type="text" name="number6"/><br />
<input type="submit" value="Send numbers"/>
</form>
而這個JavaScript方法:
var num1 = 0,
num2 = 0,
num3 = 0,
num4 = 0,
num5 = 0,
num6 = 0;
function processForm(form) {
var inputs = form.getElementsByTagName("testform");
var textValues = {};
for(var x = 0; x < inputs.length; x++) {
if(inputs[x].type != "text") {
continue;
}
textValues[inputs[x].name] = inputs[x].value;
}
num1 = textValues['number1'];
num2 = textValues['number2'];
num3 = textValues['number3'];
num4 = textValues['number4'];
num5 = textValues['number5'];
num6 = textValues['number6'];
document.write(num1);
document.write(num2);
document.write(num3);
document.write(num4);
document.write(num5);
document.write(num6);
return false;
}
當按下發送號碼:
我不明白爲什麼我得到:
undefinedundefinedundefinedu ndefinedundefinedundefined
這些未定義的變量,爲什麼他們打印在瀏覽器的另一個選項卡中?
那我用什麼呢? DIVS? – Alpha2k 2014-09-25 14:04:02
查看新的更新。 – mplungjan 2014-09-25 14:16:32