在我的應用程序中,我有五個文本框。我想要做的任務是添加(總和)五個文本框中輸入的值,並將其顯示在第6個文本框中。我已經嘗試了下面的代碼,但沒有顯示添加。你能告訴我我在這裏做錯了嗎?文本框值的動態求和asp.net
ASPX代碼
<asp:TextBox ID="D1" runat="server" Width="50px" onkeypress="return validate(event)" onkeyup="calc()" ></asp:TextBox>
<asp:TextBox ID="D2" runat="server" Width="50px" onkeypress="return validate(event)" onkeyup="calc()" ></asp:TextBox>
<asp:TextBox ID="D3" runat="server" Width="50px" onkeypress="return validate(event)" onkeyup="calc()" ></asp:TextBox>
<asp:TextBox ID="D4" runat="server" Width="50px" onkeypress="return validate(event)" onkeyup="calc()" ></asp:TextBox>
<asp:TextBox ID="D5" runat="server" Width="50px" onkeypress="return validate(event)" onkeyup="calc()" ></asp:TextBox>
Total Hours<asp:TextBox ID="Total" runat="server" Width="50px" ReadOnly="True"></asp:TextBox>
的JavaScript
<script type="text/javascript">
//Function to allow only numbers to textbox
function validate(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('D1');
var phn1 = document.getElementById('D2');
var phn2 = document.getElementById('D3');
var phn3 = document.getElementById('D4');
var phn4 = document.getElementById('D5');
//comparing pressed keycodes
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
return false;
}
}
void function calc(dat) {
var a1, a2, a3, a4, a5, total, pp;
a1 = document.getElementById("<%=D1.ClientID%>").value;
a2 = document.getElementById("<%=D2.ClientID%>").value;
a3 = document.getElementById("<%=D3.ClientID%>").value;
a4 = document.getElementById("<%=D4.ClientID%>").value;
a5 = document.getElementById("<%=D5.ClientID%>").value;
total = parseInt(a1) + parseInt(a2) + parseInt(a3) + parseInt(a4) + parseInt(a5);
document.getElementById(<%=Total%>).value = total;
}
</script>
你缺少引號'(<%=Total%>)'也許應該是'( 「%=總%>」) '像你的其他getElement *調用 –
在'validate()'方法中也使用''<%= D1.ClientID%>'.. etc'。在calc()' – mshsayem
裏面使用''<%= Total.ClientID%>''它給了我一個錯誤,'0x800a1391 - JavaScript運行時錯誤:'calc'未定義' – SPandya