繁体   English   中英

jQuery防止两次提交表单并在Enter键上提交表单

[英]jQuery preventing form from being submited twice and on enter key

我有这个表格:

<form action="/web/events/add" id="EventAddForm" method="post" accept-charset="utf-8">
    <div style="display:none;"><input type="hidden" name="_method" value="POST"/></div> 
    <div class="input text">
        <label for="AddressSearch">Search address</label>
        <input name="adress_search" type="text" id="AddressSearch"/>
    </div>
    <div class="input text required">
        <label for="EventAddress">Address</label>
        <input name="data[Event][address]" type="text" id="EventAddress"/>
    </div>
    <div class="input text required">
        <label for="EventPostNr">Post nr</label>
        <input name="data[Event][post_nr]" type="text" id="EventPostNr"/>
    </div>
    <div class="input text required">
        <label for="EventCity">City</label>
        <input name="data[Event][city]" type="text" id="EventCity"/>
    </div>
    <div class="submit">
        <input onclick="disableSubmit(this)" type="submit" value="Save"/>
    </div>
</form>

因此,有一个disableSubmit函数,该函数不允许将表单提交两次。

   function disableSubmit(b)
   {
      b.disabled = true;
      b.value = 'Saving...';
      b.form.submit();
   }

这将被调用,没有问题,但是我想包含其他功能,如果某些字段处于焦点或按下Enter键,则该功能将不允许提交表单。 这里是:

   $("#EventAddForm").submit(function( event ) {
     if ( $("#AddressSearch").is(":focus") ||
        $("#EventPostNr").is(":focus") ||
        $("#EventCity").is(":focus") ||
        event.which == 13){
             event.preventDefault();
       }
     });

在我引入disableSubmit函数之前,它曾经可以工作,现在我需要获得两个调用,但是无法弄清楚。

任何帮助或指导,我们将不胜感激。

只需保留一个防止多次提交的变量:

jQuery(function($) {
    var submitting = false;

    $("#EventAddForm").submit(function(event) {
        if (submitting) {
            event.preventDefault();
            return;
        }
        submitting = true;
        // rest of code here
    });
});

在输入字段中,将要求的单词放在此处的某处,并且不再需要调用不是disableSubmit的函数。 像这样:

<input name="adress_search" type="text" id="AddressSearch" required />

由于这些建议都没有奏效,所以我一直在研究,找到了对我有帮助的解决方案。 希望对将来的某人有用。

     var input = document.getElementById('AddressSearch'); 

        google.maps.event.addDomListener(input, 'keydown', function(e) { 
                if (e.keyCode == 13) 
                { 
                        if (e.preventDefault) 
                        { 
                                e.preventDefault(); 
                        } 
                        else 
                        { 
                                // Since the google event handler framework does not handle early IE versions, we have to do it by our self. :-( 
                                e.cancelBubble = true; 
                                e.returnValue = false; 
                        } 
                } 
        }); 

暂无
暂无

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

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