简体   繁体   English

JavaScript表单提交问题

[英]Javascript Form Submission issue

I need to get a form to submit but when it submits I need it to disable the button because the query takes a few seconds to run and the people using this form don't understand loading pages and so they will click 5 or 6 times on the button submitting the form way to many times. 我需要提交一个表单,但是在提交表单时,我需要它禁用按钮,因为查询需要运行几秒钟,并且使用此表单的人不了解加载页面,因此他们将点击5或6次该按钮多次提交表单。 I had a working method in Chrome but in IE it submits twice and I know why but I don't know how to get it to work for both. 我在Chrome浏览器中有一种有效的方法,但是在IE中它提交了两次,我知道为什么,但是我不知道如何使它同时适用于两者。 Here is the form line: 这是表格行:

    <form method="post" id="submitItem" action="secondPage.php">

I have tried various versions of this such as using onSubmit or other related ideas but none work. 我已经尝试过各种版本,例如使用onSubmit或其他相关思路,但是没有用。 The "Working" solution in chrome that submits twice in IE is that form call and the following JS: 在IE中提交两次的chrome中的“工作”解决方案是该表单调用和以下JS:

$( document ).on( "click", ".once-only", function(){
    $(".once-only").prop('disabled', true);
    $('form').submit();
});

The class "once-only" is attached to the submit button so upon clicking submit that button gets disabled in both browsers but because I have "action='....'" in the form instantiation line it submits using that and the ".submit()". “仅一次”类附加到了“提交”按钮上,因此在单击“提交”后,在两个浏览器中都禁用了该按钮,但是由于我在表单实例化行中使用了“ action ='....'”,因此它使用“ 。提交()”。 Chrome does not submit using the action but only the ".submit()". Chrome不会使用操作提交,而仅使用“ .submit()”提交。 Is there a way to get this working? 有没有办法让这个工作? I have tried a large combination of changing the JS to use a function or taking out the ".submit()" or even changing what is in the form line but I haven't figured one out. 我已经尝试过将JS更改为使用函数或取出“ .submit()”,甚至更改表格行中的内容的大组合,但是我还没有弄清楚。 Any ideas? 有任何想法吗? IE has been giving me problems with this site anytime I use JS, the AJAX only works in chrome for all my other pages so I REALLY hate using IE but more than half the people using the site don't even know what chrome is. 每当我使用JS时,IE都给我带来了这个网站的问题,AJAX在其他所有页面上仅适用于chrome,所以我真的很讨厌使用IE,但是超过半数的使用该网站的人甚至不知道chrome是什么。 Any tips? 有小费吗? I can share any other code if needed! 如果需要,我可以共享任何其他代码!

Try this: 尝试这个:

$( document ).on( "click", ".once-only", function(event) {
  event.preventDefault(); //this should disable the default form submit action
  $(".once-only").prop('disabled', true);
  $('form').submit();
});

but in IE it submits twice 但在IE中,它会提交两次

For that you can first off or unbind the click followed by on 对于您可以先off或者取消绑定点击之后on

// If the button is not dynamically generated , there is no need to delegate the event
$('.once-only).off('click').on( "click",function(event){
   // Rest of the code
})

For second issue 第二期

you can either use button type = "button" then use ajax instead of form action 您可以使用button type = "button"然后使用ajax代替form action

Alternately you can use button type="submit" 您也可以使用button type="submit"

$('.once-only').off('click').on( "click",function(event) {
  event.preventDefault(); //prevent default behavior 
  $(".once-only").prop('disabled', true);
   $.ajax({
    url:'some url'
    //Rest of code
    })
});

Note ajax also works in IE 注意ajax也可以在IE中使用

edit 编辑

or like this: onclick="this.form.submit();this.enabled=false;" 或类似这样: onclick="this.form.submit();this.enabled=false;"


so the real answer is this 所以真正的答案是这样

onclick="setTimeout((function(){ this.disabled = true; }).bind(this),0);"

explanation : this instructs the browser to disable the button later; 说明 :指示浏览器稍后禁用按钮; since JS works in event loop, you enqueue a time-out event that'll do it outside that onclick event. 由于JS在事件循环中工作,因此您需要排队一个超时事件,该事件将在onclick事件之外进行。

i think it is just stupid of chrome to make me do this trick. 我认为让我做这招只是愚蠢的铬。

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

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