我正在使用函數添加兩個文本框,並希望在第三個文本框中顯示結果。爲此,我使用了兩個函數。第一個getdata()
要讀取第一個文本框的值,第二個是getdata1()
以讀取第二個文本框的值。我也使用onchange事件。但結果顯示爲NaN
。請幫幫我。 這裏是我的代碼與java腳本函數問題
var a, b, c;
function getdata(txt) {
x = txt.value;
if (!isNaN(x)) {
a = parseInt(document.getElementById("txt").value);
} else {
alert("Input not valid");
txt.focus();
txt.value = "";
}
}
function getdata1(txt) {
x = txt.value;
if (!isNaN(x)) {
b = parseInt(document.getElementById("txt").value);
} else {
alert("Input not valid");
txt.focus();
txt.value = "";
}
}
function myFunction(s, t) {
var s = a;
var t = b;
var c = s + t;
document.getElementById("result").value = c;
}
<td><input type="text" name="txt[]" id="txt[]" onchange="getdata(this)" /></td>
<td><input type="text" name="txt[]" id="txt[]" onchange="getdata1(this)" /></td>
<td><input type="text" name="result" id="result" onfocus="myFunction()" /></td>
寫問題和代碼,並提供你的HTML代碼 – Bhargav
你已經給出相同的'id'這是其中一個原因這兩個文本框對於錯誤 – warl0ck
https://stackoverflow.com/help/how-to-ask 閱讀此!並編輯你的問題。 – amighty