當我得到用戶輸入並單擊提交時,它顯示x = undefined。如何解決這個問題? 這是我的代碼。或http://jsfiddle.net/L9LKc/如何在javascript中獲取x = undefined的騎乘版
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JS Assignment</title>
<script>
function changeCheckBox() {
try {
var max = document.myform.check.length;
var count = 0;
for (var i = 0; i < max; i++) {
if (document.myform.check[i].checked == true) {
count++;
serNoChecked = i;
}
}
if (count == 1) {
for (var i = 0; i < max; i++) {
if (document.myform.check[i].checked == false) {
document.myform.check[i].disabled = true;
}
}
} else if (count == 0) {
for (var i = 0; i < max; i++) {
document.myform.check[i].disabled = false;
}
}
if (null == max) return false;
if (count == 0) {
return true;
} else if (count > 0) {
return false;
}
} catch (e) {
alert(e.message);
}
}
</script>
<script>
function arith(op) {
var n1 = parseInt(document.getElementById('num1').value,10);
var n2 = parseInt(document.getElementById('num2').value,10);
var newVal;
if (op == "Addition") {
newVal = n1+n2;
} else if (op == "Subtraction") {
newVal = n1-n2;
} else if (op == "Multiplication") {
newVal = n1*n2;
} else if (op == "Division") {
newVal = n1/n2;
}
var demoP=document.getElementById("demo")
demoP.innerHTML="x=" + newVal;
}
</script>
</head>
<body background="photo.jpg">
<h3>Simple JavaScript Arithmetic Operations</h3>
<form name="myform" method="post" onsubmit="return arith()">
Value 1 <input type ="text" id="num1"> <br><br>
Value 2 <input type="text" id="num2"> <br><br>
<input type="checkbox" name="check" value="Addition" id="check1" onclick="changeCheckBox(this.value)">Addition<br>
<input type="checkbox" name="check" value="Subtraction" id="check2" onclick="changeCheckBox(this.value)">Subtraction<br>
<input type="checkbox" name="check" value="Multiplication" id="check3" onclick="changeCheckBox(this.value)">Multiplication<br>
<input type="checkbox" name="check" value="Division" id="check4" onclick="changeCheckBox(this.value)">Division<br><br>
<input type="submit" value="Submit">
</form>
<p id="demo"></p>
</body>
</html>
你'op'是'undefined'因爲你當你調用'onsubmit =「時返回arith()' –
Ya正在處理,但是我不能得到輸出結果。你可以檢查它嗎? –