用 jquery 替换 onsubmit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9441531/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:49:10  来源:igfitidea点击:

Replace onsubmit with jquery

jqueryformsonsubmit

提问by hd.

I have these code below :

我在下面有这些代码:

<form name="submissions" action="/" onsubmit="window.open('', 'foo', 'width=900,height=900,status=yes,resizable=yes,scrollbars=yes')" target="foo" method="post">
.......
<input type="submit" id="printbtn" name="printbtn" value="Print" />
<input type="submit" id="editbtn" name="editbtn" value="Edit" />
</form>

* Edited *

* 编辑 *

the window.open should only happens when the Print button is clicked. I want to remove onsubmit() event and do it with jQuery. How can I do it?

window.open 应该只在点击 Print 按钮时发生。我想删除 onsubmit() 事件并使用 jQuery 来完成。我该怎么做?

回答by jcreamer898

Will this work?

这会起作用吗?

$('form[name="submissions"]').submit(function(event){
     event.preventDefault();
     // Do other stuff   
});

UPDATE: Actually, try this out...

更新:实际上,试试这个......

$(function(){
    $('form[name="submissions"]').removeAttr('onsubmit')
        .submit(function(event){
            event.preventDefault();
                    // This cancels the event...
        });
    // This should fire your window opener...
    $('#printbtn').click(function(){
        window.open('', 'foo', 'width=900,height=900,status=yes,resizable=yes,scrollbars=yes');
    })

});

Should remove the onsubmit, and prevent the form from submitting...

应该删除onsubmit,并阻止表单提交...

回答by VadimAlekseev

$('form[name=submission]').submit(function(e) {
  e.preventDefault();
  //your code
});

Hope this helps.

希望这可以帮助。

回答by dead

 $("#btn").on('click', function(e) {
  window.open('', 'foo', 'width=900,height=900,status=yes,resizable=yes,scrollbars=yes');
  e.preventDefault();

 });

回答by Joe Landsman

If you just want to remove the onsubmit function then I think this should do the trick.

如果您只想删除 onsubmit 功能,那么我认为这应该可以解决问题。

$("form[name='submissions']").attr('onsubmit', '');