javascript jquery按钮触发服务器端onclick事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10557021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery button trigger server side onclick event
提问by pita
I am using a jquery dialog, when user click ok, the server side onclick event should be fired, if click cancel, nothing happened.
我正在使用 jquery 对话框,当用户单击确定时,应该触发服务器端 onclick 事件,如果单击取消,则什么也没有发生。
i have to prevent the click function at the beginning by preventDefault() function.
我必须在开始时通过 preventDefault() 函数阻止点击功能。
$(document).ready(function () {
$("#<%=submit.ClientID %>").click(function (event) {
event.preventDefault();
$("#confirm").dialog({
buttons: {
"OK": function () { $(this).dialog("close");
Here should be the code to trigger the server side click event
},
"Cancel": function () { $(this).dialog("close");},
}
});
});
});
I don't know how to trigger a server side onclick event. any ideas? thanks
我不知道如何触发服务器端 onclick 事件。有任何想法吗?谢谢
采纳答案by pita
https://stackoverflow.com/a/10583339/1202242
https://stackoverflow.com/a/10583339/1202242
I used two button, and one is hidden. It solved my problem perfectly
我用了两个按钮,一个是隐藏的。它完美地解决了我的问题
回答by lucuma
Put the event.preventDefault()
in the cancel part or some kind of condition for it so it isn't running on every click.
将 放在event.preventDefault()
取消部分或某种条件中,这样它就不会在每次点击时运行。
回答by Sagiv Ofek
i think you are confusing between server and client sides. if you want to trigger event on the server side you need to notify him (can be done by ajax call). the click event is a client side event that will not do anything on the server side. try to put some code under the "OK" function to notify the server whatever you want like via ajaxcall.
我认为您在服务器端和客户端之间感到困惑。如果你想在服务器端触发事件,你需要通知他(可以通过ajax调用来完成)。click 事件是一个客户端事件,不会在服务器端做任何事情。尝试将一些代码放在“OK”函数下,以通过ajax调用通知服务器您想要的任何内容。
anyway you should move the event.preventDefault()
call into the "Cancel" function.
无论如何,您应该将event.preventDefault()
呼叫移动到“取消”功能中。
edit: another way to approach it is to prevent the submit to happen if you don't want it. at your form tag add onsubmit="foo()"
and define:
编辑:另一种方法是防止提交,如果你不想要它。在您的表单标签处添加onsubmit="foo()"
并定义:
function foo(){
//call your dialog and return true to continue the submit and false to cancel.
}
回答by Adriano Repetti
It looks a little bit dependant on implementation details (even if they're so widely used that they won't be changed) but code is this:
它看起来有点依赖于实现细节(即使它们被广泛使用以至于它们不会被改变)但是代码是这样的:
__doPostBack('submit','OnClick');
This will execute the OnClick
event handler for the control named submit
. As reference take a look to this little tutorial about how postbacks works in ASP.NET.
这将为OnClick
名为 的控件执行事件处理程序submit
。作为参考,看看这个关于回发如何在 ASP.NET 中工作的小教程。