是否有任何建议可以设置默认输入按钮,该按钮不仅可以在 IE 中使用,还可以在 Firefox、Chrome、Safari 等中使用。我有许多面板都设置了默认按钮,但是当我在非 ie 浏览器中按 Enter 时,它只是默认为页面上的第一个链接。
有什么建议么?
是否有任何建议可以设置默认输入按钮,该按钮不仅可以在 IE 中使用,还可以在 Firefox、Chrome、Safari 等中使用。我有许多面板都设置了默认按钮,但是当我在非 ie 浏览器中按 Enter 时,它只是默认为页面上的第一个链接。
有什么建议么?
I've provided 2 examples here, the first one using jQuery while the second uses old school javascript.
jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$("document").ready(function(){
$(".searchBox").keypress(function(e){
if (e.keyCode == 13)
//add code here
});
});
</script>
<div class="searchBox">
<input type="text" name="searchText"/>
<input type="submit" value="search now" />
</div>
Pure javascript
I've done this before using the "onkeypress" action on a div that surrounds the form. This was tested in IE7, Firefox3, and Chrome.
here's an example:
<div id="searchBox" onkeypress="return clickButton(event,'btnSearch')">
<input name="searchText" type="text" id="searchText" />
<input type="button" name="btnSearch" value="Search" id="btnSearch" onClick="SubmitAction()" />
</div>
<script type="text/javascript" language="javascript">
function SubmitAction()
{
alert('button clicked');
}
function clickButton(e, btId){
var bt = document.getElementById(btId);
if (typeof bt == 'object'){
if(navigator.appName.indexOf("Netscape")>(-1)){
if (e.keyCode == 13){
bt.click();
return false;
}
}
if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){
if (event.keyCode == 13){
bt.click();
event.cancelBubble = true;
return false;
}
}
}
}
</script>
You may have to edit this to make it fully compatible across all browsers but you should get the idea from it. Essentially any keypress while this div is focussed will call the clickButton() function. As mentioned by other answers the e.keyCode of 13 represents the Enter keypress.
您可以编写一些 javascript 来检查按下的是哪个按钮 - 如果它是 enter 键,则执行您想要的操作。
检查输入,例如:
function checkEnter()
{
if(event)
{
if(evnet.keyCode == 13) //ur code
}
else if(evt)
{
if(evt.keyCode == 13) //ur code
}
}
I added the panel control and it worked fine on mine.
<asp:Panel ID="panSearch" runat="server"
DefaultButton="btnSearch2" Width="100%" >
<table width="100%">
<tr>
<td>First Name</td>
<td ><asp:TextBox ID="txtboxFirstName2"
runat="server" ></asp:TextBox></td>
</tr>
You can also go through this link