問題描述
我只想在 Web 用戶控件中添加一些客戶端(JQuery Javascript)驗證.我放置了一個 OnClientClick 處理程序并調用了該函數.但是,即使我返回false",OnClick 方法總是會被觸發.我做錯了什么?
I just want to add some client side (JQuery Javascript) validation in a web user control. I put an OnClientClick handler and the function gets called. BUT, even if I return "false", the OnClick method always get fired. What am I doing wrong ?
我使用的是 VS 2010,目標是使用 JQuery 1.4.2 的 4.0 框架.和 JQuery UI 1.8.4.
I'm with VS 2010, targeting the 4.0 framework with JQuery 1.4.2. and JQuery UI 1.8.4.
這是一個示例代碼:
<td style="text-align:right"><asp:Button ID="btnAddSave" OnClientClick="return ValidateMail();" OnClick="btnAddSave_Click" runat="server" Text="Submit" /></td>
腳本方法:
function ValidateMail() {
alert("Bouton clicked");
return false;
}
如果我在 Page_Load 事件中設置斷點,我看到我進入并且 btnAddSave_Click 事件也被執行.
If I put a breakpoint in the Page_Load event, I see that I get in and the btnAddSave_Click event is also executed.
推薦答案
你有沒有返回 true
的點擊事件處理程序(通過 jquery 注冊)?在這種情況下,OnClientClick
的返回值將被忽略.
Do you have a click event handler (registered via jquery) which returns true
? In that case, the return value of OnClientClick
is ignored.
看看我的問題(和答案):為什么 OnClientClick 不返回 false 取消回發
Have a look at my question (and answer): Why doesn't returning false from OnClientClick cancel the postback
這篇關于ASP.NET OnClientClick="return false;"不工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!