为什么不从的OnClientClick返回false取消回传 [英] Why doesn't returning false from OnClientClick cancel the postback
问题描述
我有一个的LinkButton
在这里我使用的OnClientClick
属性来问他是否真的想执行一个用户动作,例如:
<脚本>
功能confirmDelete(){
返回确认('你真的要删除吗?');
}
< / SCRIPT>< ASP:LinkButton的=服务器的OnClientClick =返回confirmDelete()... />
此模式通常工作,但这个特定页面上,事实并非如此。无论我点击确定或确认对话框取消,回传被执行。
只是为了完整性(回答PST的问题):渲染HTML就可以了。例如。它看起来是这样的:
<一个ID =ctl00_c1_content_btnDelete的onclick =返回confirmDelete();
HREF =JavaScript的:WebForm_DoPostBackWithOptions(新WebForm_PostBackOptions(..))
...>
删除
&所述; / A>
的原因的行为是另一块的JavaScript,其中的链接(按钮)的click事件的处理程序是通过jQuery的注册,例如类似这样的内容:
<脚本>
$(文件)。就绪(函数(){
$('A')。点击(函数(){
// ...
返回(someCondition == TRUE);
});
});
< / SCRIPT>
看来这单击处理程序是由的OnClientClick
注册一前一后调用,而当这一个返回真正
,然后回发发生,独立于第一点击处理器(确认对话框)的结果。
I have a LinkButton
where I use the OnClientClick
property to ask the user whether he really wants to perform an action, e.g:
<script>
function confirmDelete() {
return confirm('Do you really want to delete?');
}
</script>
<asp:LinkButton runat="server" OnClientClick="return confirmDelete()" ... />
This pattern usually works, but on this specific page, it doesn't. No matter whether I click OK or Cancel in the confirm dialog, the postback is executed.
Just for completeness (to answer pst's question): the rendered HTML is OK. E.g. it looks like this:
<a id="ctl00_c1_content_btnDelete" onclick="return confirmDelete();"
href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(..))"
... >
Delete
</a>
The reason for the behavior was another piece of javascript, where a handler for the link(button)'s click event was registered via jquery, e.g. something similar to this:
<script>
$(document).ready(function() {
$('a').click(function() {
// ...
return (someCondition == true);
});
});
</script>
It seems this click-handler was called after the one registered by OnClientClick
, and when this one returned true
, then the postback occurred, independent of the result of the first click-handler (the confirm dialog).
这篇关于为什么不从的OnClientClick返回false取消回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!