简体   繁体   English

如何在jquery中阻止表单提交行为?

[英]How to prevent form on submit behavior in jquery?

I need to get an .click() event and prevent form from behaving as it was initially designed and make my own changes and submit. 我需要获得一个.click()事件,并防止表单按照最初设计的方式运行,并进行自己的更改并提交。 How is it possible? 这怎么可能?

EDIT: Form input actually has an onclick defined behavior. 编辑:表单输入实际上有一个onclick定义的行为。 I need to redefine it somehow. 我需要以某种方式重新定义它。

EDIT: Some code 编辑:一些代码

<form action='link' method='get'>
   <input type="image" name="name" id="id" class="class" onclick="this.form.action='some_link'" title="Title" value="" src="image.gif">
</form>

If you don't want it to submit the form, return false at the end of your click function. 如果您不希望它提交表单,请在单击功能结束时return false If you want to submit the form, use the form element and call the submit function on it: 如果要提交表单,请使用表单元素并在其上调用submit函数:

$('#myFormID').submit();
$('#form').submit(function(e) {
    // Some code
});

I think it's enough. 我认为这就够了。

You could use something like this (haven't tested it but it should work) 你可以使用这样的东西(没有测试它但它应该工作)

form = $("#yourform");
button = $("#yoursubmit");
button.click(function(){
             dosomething();
             form.submit();
             return false;});

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

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