如何使 asp:button 成为向 asp:textbox 输入文本时的默认按钮

发布于 2024-11-28 10:49:24 字数 1022 浏览 4 评论 0原文

在文本框中输入文本后如何设置默认按钮。这是我到目前为止所拥有的。但它不起作用

<td>
                    <asp:Label ID="displayrowLabel" runat="server" Text="# of Rows Displayed:"></asp:Label>
                    <asp:TextBox ID="displayRowQuery" runat="server"></asp:TextBox>
                    <asp:Button ID="displayRowButton" runat="server" Text="Click" OnClick="ddlPageItems_SelectedIndexChanged" />
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="displayRowQuery"  ValidationExpression="[1-9][0-9]*"  ErrorMessage="Wrong Input" />
                </td>               
            </tr>
        </table>            
    </PagerTemplate>

我尝试添加一些 jquery 来处理这个问题:

<script type="text/javascript">
    $("displayRowQuery").keyup(function (event) {
        if (event.keyCode == 13) {
            $("displayRowButton").click();
        }
    });

How can I set default button after text is entered into a text box. This is what I have so far. But it doesn't work

<td>
                    <asp:Label ID="displayrowLabel" runat="server" Text="# of Rows Displayed:"></asp:Label>
                    <asp:TextBox ID="displayRowQuery" runat="server"></asp:TextBox>
                    <asp:Button ID="displayRowButton" runat="server" Text="Click" OnClick="ddlPageItems_SelectedIndexChanged" />
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="displayRowQuery"  ValidationExpression="[1-9][0-9]*"  ErrorMessage="Wrong Input" />
                </td>               
            </tr>
        </table>            
    </PagerTemplate>

I tried adding some jquery to handle this:

<script type="text/javascript">
    $("displayRowQuery").keyup(function (event) {
        if (event.keyCode == 13) {
            $("displayRowButton").click();
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-05 10:49:24

如果您有一组需要默认按钮的控件,您还可以为 asp:panel 设置默认按钮。

基本上:

<asp:Panel ID="aPanel" runat="server" DefaultButton="btnSubmit2">

    <!-- Some Controls Here -->

    <asp:Button UseSubmitBehavior="true" ID="btnSubmit2" Text="Submit" runat="server" onclick="btnSubmit2_Click" />

</asp:Panel>

http://www.aspnettutorials.com/tutorials/controls/defaultbutton -面板-aspnet.aspx

You can also set a default button for an asp:panel if you have a group of controls you need a default button for.

Basically:

<asp:Panel ID="aPanel" runat="server" DefaultButton="btnSubmit2">

    <!-- Some Controls Here -->

    <asp:Button UseSubmitBehavior="true" ID="btnSubmit2" Text="Submit" runat="server" onclick="btnSubmit2_Click" />

</asp:Panel>

http://www.aspnettutorials.com/tutorials/controls/defaultbutton-panel-aspnet.aspx

冰火雁神 2024-12-05 10:49:24

您的 id 选择器中缺少“#”:

    $("#displayRowQuery").keyup(function (event) {
        if (event.keyCode == 13) {
            $("#displayRowButton").click();
        }
    });

编辑: 正如 @Nicolás 所指出的,您可能必须用 ClientId 替换 id 选择器,如下所示:

$("#displayRowQuery") => $("#<% = displayRowQuery.ClientId %>") 
$("#displayRowButton") >= $("#<% = displayRowButton.ClientId %>")

You're missing the "#" in your id selector:

    $("#displayRowQuery").keyup(function (event) {
        if (event.keyCode == 13) {
            $("#displayRowButton").click();
        }
    });

Edit: As @Nicolás has pointed out, you may have to substitute the id selectors with ClientId like this:

$("#displayRowQuery") => $("#<% = displayRowQuery.ClientId %>") 
$("#displayRowButton") >= $("#<% = displayRowButton.ClientId %>")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文