实现在表单内按回车键,执行指定按钮的事件

<script language="javascript">  

function WebForm_FireDefaultButton(event, target) {
        if (event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
        var defaultButton = document.getElementById(target);
        if (defaultButton && typeof(defaultButton.click) != "undefined") {
            defaultButton.click();
            event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}

</script>

<form id="form1" runat="server" method=post onkeypress="javascript:return WebForm_FireDefaultButton(event,'btnTest2');">

   <asp:Button ID=btnTest1 runat=server Text="test1"
            OnClientClick='alert("test1");' ></asp:Button>
      
   <asp:Button ID=btnTest2 runat=server Text="test2"
            OnClientClick='alert("test2");' ></asp:Button>

</form>

 

posted on 2009-06-16 10:29  Dot-Boy  阅读(322)  评论(0编辑  收藏  举报