如何在文本框中输入文本后设置默认按钮。这就是我到目前为止所拥有的。但它不起作用
<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();
}
});
答案 0 :(得分:2)
如果您有一组需要默认按钮的控件,您还可以为asp:面板设置默认按钮。
基本上:
<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
答案 1 :(得分:0)
您的ID选择器中缺少“#”:
$("#displayRowQuery").keyup(function (event) {
if (event.keyCode == 13) {
$("#displayRowButton").click();
}
});
编辑正如@Nicolás指出的那样,您可能需要使用ClientId
替换id选择器,如下所示:
$("#displayRowQuery") => $("#<% = displayRowQuery.ClientId %>")
$("#displayRowButton") >= $("#<% = displayRowButton.ClientId %>")