Javascript 如何制作asp文本框的按键事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12094336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make keypress event of asp textbox?
提问by user968441
Hi all i am writing calculation of varius asp textbox controls. I want my calculation being done with keypress event. Below code i am using but not working
大家好,我正在编写各种 asp 文本框控件的计算。我希望我的计算是通过按键事件完成的。下面的代码我正在使用但不工作
.aspx page
.aspx 页面
<asp:TextBox ID="txtMaintCost onkeypress="calculateFinanceDetail(); return false;" runat="server"></asp:TextBox>
.js file
.js 文件
function calculateFinanceDetail() {
var txtMaintCost = $('input[id$=txtMaintCost]').val();
var txtInstallCost = $('input[id$=txtInstallCost]').val();
var txtFreightCost = $('input[id$=txtFreightCost]').val();
}
its not calling javascript function on keypress event... If anyone have any idea than please help me in this..
它没有在按键事件上调用 javascript 函数......如果有人有任何想法,请帮助我。
回答by Adil
Missing "
at the end of id of textbox.
"
在文本框的 id 末尾丢失。
Change
改变
<asp:TextBox ID="txtMaintCost onkeypress="calculateFinanceDetail(); return false;" runat="server"></asp:TextBox>
To
到
<asp:TextBox ID="txtMaintCost" onkeypress="calculateFinanceDetail(); return false;" runat="server"></asp:TextBox>
Try using the ClientID of server controls. You might not having static idsfor server side controls. You do not have to use wild cards if you have fixed ids.
尝试使用服务器控件的 ClientID。您可能没有服务器端控件的静态 ID。如果您有固定 ID,则不必使用通配符。
function calculateFinanceDetail() {
var txtMaintCost = $('input[id=<%=txtMaintCost.ClientID%>]').val();
var txtInstallCost = $('input[id=<%=txtInstallCost.ClientID%>]').val();
var txtFreightCost = $('input[id=<%=txtFreightCost.ClientID%>]').val();
}
回答by Elias Van Ootegem
You're missing quotes here ID="txtMaintCost onkeypress="
, it should be ID="txtMaintCost" "onkeypress="
你在这里缺少引号ID="txtMaintCost onkeypress="
,应该是ID="txtMaintCost" "onkeypress="