简体   繁体   English

jQuery按钮触发服务器端onclick事件

[英]jquery button trigger server side onclick event

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()函数来防止click函数。

  $(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. 我不知道如何触发服务器端onclick事件。 any ideas? 有任何想法吗? thanks 谢谢

event.preventDefault()放在取消部分或某种条件下,以便它不会在每次单击时都运行。

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). 如果要在服务器端触发事件,则需要通知他(可以通过ajax调用来完成)。 the click event is a client side event that will not do anything on the server side. click事件是客户端事件,不会在服务器端执行任何操作。 try to put some code under the "OK" function to notify the server whatever you want like via ajax call. 尝试在“确定”功能下放置一些代码,以通过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.
}

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 . 这将执行OnClick事件处理程序命名为控制submit As reference take a look to this little tutorial about how postbacks works in ASP.NET . 作为参考,请看一下有关回发在ASP.NET中如何工作的小教程。

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 它完美地解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM