admin管理员组文章数量:1287621
Good Morning...
I am using java script in each page to trigger the Enter key press Event inside the textbox.
It is working fine. Now i want to place the code in the .js
file for global access.
function EnterKeyPress(id,e) {
// look for window.event in case event isn't passed in
if (window.event) { e = window.event; }
if (e.keyCode == 13 || e.keyCode == 121) {
window.document.getElementById('<%= ibtnSubmit.ClientID %>').click();
}
}
I dont want to hard code the control id. Can anybody help me please..
Good Morning...
I am using java script in each page to trigger the Enter key press Event inside the textbox.
It is working fine. Now i want to place the code in the .js
file for global access.
function EnterKeyPress(id,e) {
// look for window.event in case event isn't passed in
if (window.event) { e = window.event; }
if (e.keyCode == 13 || e.keyCode == 121) {
window.document.getElementById('<%= ibtnSubmit.ClientID %>').click();
}
}
I dont want to hard code the control id. Can anybody help me please..
Share Improve this question edited Mar 19, 2015 at 7:12 Jayan 18.5k15 gold badges92 silver badges146 bronze badges asked Aug 5, 2009 at 5:20 GeethaGeetha3 Answers
Reset to default 9You can use your id parameter instead of the ControlID, and when you use this function in your pages, you can pass the ControlID as a parameter:
function EnterKeyPress(id,e) {
if (window.event) { e = window.event; }
if (e.keyCode == 13 || e.keyCode == 121) {
document.getElementById(id).click();
}
}
You are using ASP .NET, so in your code-behind you can assign the keypress event:
TextBox1.Attributes.Add("onkeypress",
string.Format("EnterPressKey('{0}', event);", ibtnSubmit.ClientID));
But wait, that functionality is already done on ASP .NET, you can use ASP:Panel controls to wrap your mon controls, and you can assign a DefaultButton for the panel to indicate which button gets clicked when the Panel control has focus and the user presses the Enter key.
If you're using jQuery you can do it like this:
$("input[id*=ibtnSubmit]").click()
If you want the form to submit when the user presses the Enter key, then you don't need javascript. It will do so automatically. Just put the textbox inside a form element with an action:
<form action="process.php">
<input type="text">when the enter key is pressed, the form is submitted and sent to process.php</input>
</form>
本文标签: aspnetJavascriptEnter Key PressStack Overflow
版权声明:本文标题:asp.net - Javascript : Enter Key Press - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741259896a2367412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论