我有一个asp.net按钮,它的UseSubmitBehavior="false",就像这样:
<asp:Button ID="btnLogin" runat="server" UseSubmitBehavior="false" OnClientClick="if (!LoginPopup()) {return false;}" CssClass="login-button" Text="<%$ Resources: HRGELoggedOutMaster, Login %>"> </asp:Button>
以及像这样的JavaScript函数:
function LoginPopup() {
var username = $("#<%= UsernameTextBox.ClientID %>").val();
var password = $("#<%= PasswordTextBox.ClientID %>").val();
var remMe;
if ($('#<%= chkRememberMe.ClientID %>').attr('checked') == "checked") {
remMe = true;
}
else {
remMe = false;
}
if (username != '' && password != '')
CallPageMethod("LoginPopup", ["username", username, "password", password, "rememberMe", remMe], LoginPopupSucceeded, LoginPopupFailed);
else
alert("Invalid username/password");
return false;
}
我将其在文本框的按键事件中调用,代码如下:
$("#<%=this.UsernameTextBox.ClientID%>").keypress(function (event) {
fireButtonClick($("#<%=this.btnLogin.ClientID%>"), event);
});
function fireButtonClick(ctrl, event) {
if (event.which || event.keyCode) {
if ((event.which == 13) || (event.keyCode == 13)) {
ctrl.click();
return false;
}
我希望在输入无效时停止postback,但这并没有起作用。请建议我如何停止postback。
UseSubmitBehavior
吗?你可以摆脱它并明确定义OnClick
。只有在OnClientClick
返回 true 时,OnClick
才会触发。 - Abhitalks