JS实现给页面表单设置触发默认按钮

var defaultBtnId;
function setDefaultButton(id) {
    defaultBtnId = id;
}
document.onkeydown = function (e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 13) {
        if (typeof (defaultBtnId) != "undefined" && defaultBtnId != null) {
            $(":input[id$=‘" + defaultBtnId + "‘].OperationButton").click();
            if (typeof (window.event) != "undefined") {
                window.event.returnValue = false;
            }
        }
    }
}

使用(在表单域所在的DIV上加上onclick触发的按钮ID就行, 可以是动态ID):

                    <div class="border" onclick="setDefaultButton(‘SearchButton‘)">
                            <table class="noborder">
                                <tr>
                                    <td class="label">
                                        <asp:Label runat="server" ID="EmployeeIdTitleLabel" Text="Employee ID"></asp:Label><em>*</em>
                                    </td>
                                    <td>
                                        <asp:TextBox runat="server" ID="EmployeeIdTextBox" CssClass="RequiredText" MaxLength="6"
                                            SkinID="IntegerRequired" Width="60px"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="EmployeeIdTextBoxRequiredFieldValidator" runat="server"
                                            ControlToValidate="EmployeeIdTextBox" ValidationGroup="Search" ErrorMessage="[Employee ID] is required, please re-input."
                                            Display="None"></asp:RequiredFieldValidator>
                                    </td>
                                    <td>
                                        <asp:Button runat="server" SkinID="OperationButton" Text="Search" OnClick="SearchButton_Click"
                                            OnClientClick="return CallValidate(‘Search‘);" ID="SearchButton" ValidationGroup="Search"
                                            CommandName="Search" />
                                    </td>
                                </tr>
                            </table>
                        </div>

JS实现给页面表单设置触发默认按钮,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。