停止然后再重新启动阿贾克斯检查后提交()然后再、重新启动、阿贾克斯

2023-09-11 01:14:59 作者:犯贱请绕道

我问一个问题[ here]最近,它只是不给我提供了一个答案。这就是我想要做的,你可以看到我在上面的链接第一次尝试:

在用户提交表单 停止默认提交操作 检查,看是否有类似的条目存在于数据库 如果它确实,显示通知,询问他们是否仍要提交,并给予一个选项,让他们无论如何提交(启用默认操作并提交)。 如果它没有,启用默认行为的形式,让它提交

我不知所措。任何帮助是AP preciated。由于团伙。

编辑西蒙:这是我使用的是与你的解决方案在code:

  VAR forceSubmitForm = FALSE;
$('#purchaser_contact)。递交(函数(){

  如果(forceSubmitForm)返回true;

  如果($(#ID)。VAL()==){

    如果($(#FNAME)。VAL()==''|| $(#城市)。VAL()==''){警报(First Name和城市为必填项。请填写以下在继续之前)。返回false; }

      $阿贾克斯({
        键入:POST,
        网址:AJAX / contactSearch.php',
        数据:({FNAME:$(#FNAME)VAL(),LNAME:$(#L-NAME)VAL(),城市:$(#城)VAL(),状态:$( #state)。VAL()}),
        成功:函数(D){
            VAR OBJ = JSON.parse(D);
            如果(obj.result!= 0){
            $(#contactSearch)删除()。
            $(#键 - 包装)之前('< D​​IV ID =contactSearch>+ obj.result +'< / DIV>');
            $(#contactSearch)的slideToggle(慢)。
          } 其他 {
            forceSubmitForm = TRUE;
            $('#purchaser_contact)提交()。 //表将提交
          }
        }
      });

      返回false;

  } // END IF空号

});
 

这将不会工作,直到推提交按钮第二次(即 $('#purchaser_contact)提交(); 未提交表单)。

修改这对我,但西面的答案从来没有工作,是最接近的,他声称,它适用于所有主要的浏览器,所以如果它可以帮助别人,太棒了。

解决方案

这是您正在寻找的解决方案。

  VAR forceSubmitForm = FALSE;
$('#myForm会)。递交(函数(){
    如果(forceSubmitForm)返回true;
    $阿贾克斯({
        // ...
        成功:功能(数据){
            如果(data.entry!='OK'){//或什么检查
                forceSubmitForm = TRUE;
                $('#myForm会)提交()。 //表将提交
            }其他{
                //表单不会被提交
            }
        }
    });
    返回false;
});
 
阿贾主帅 不会人盯人防守C罗 尤文风格不同皇马

修改 为了证明自己,这种方法的工作原理,我刚刚创建了一个示例页面的表单和一个提交按钮。这在所有主流浏览器正常工作:

  $(函数(){//在DOM准备就绪
    VAR forceSubmitForm = FALSE;

    $('#purchaser_contact)。住('提交',函数(){
        如果(forceSubmitForm){
            警报('提交');
            返回true;
        }

        //回调函数相同的方式工作作为超时

        的setTimeout(函数(){
            forceSubmitForm = TRUE;
            $('#purchaser_contact)提交()。
        },1500); 1,5秒后//提交

        返回false; //不要马上提交
    });
});
 

I asked a question [here] recently and it's just not providing me with an answer. Here's what I want to do and you can see my first attempt at the link above:

User submits form Stop default submit action check to see if a similar entry exists in database

If it does, display a notice asking them if they want to submit anyway and give an option to let them submit anyway (enable default action and submit it). If it does not, enable the default action on the form and let it submit

I'm at a loss. Any help is appreciated. Thanks gang.

EDIT for Simeon: Here is the code I'm using with your solution:

var forceSubmitForm = false;
$('#purchaser_contact').submit(function(){

  if(forceSubmitForm) return true;

  if( $("#id").val()=="" ){

    if( $("#fname").val()=='' || $("#city").val()=='' ){ alert('First Name and City are required fields. Please fill these in before continuing.'); return false; }

      $.ajax({
        type: "POST",
        url: 'ajax/contactSearch.php',
        data: ({ fname: $("#fname").val(), lname: $("#lname").val(), city: $("#city").val(), state: $("#state").val() }),
        success: function(d) {
            var obj = JSON.parse( d );
            if(obj.result != 0){
            $("#contactSearch").remove();
            $("#button-wrapper").before('<div id="contactSearch">'+obj.result+'</div>');
            $("#contactSearch").slideToggle('slow');                                
          } else {
            forceSubmitForm = true;
            $('#purchaser_contact').submit(); // Form will submit
          }
        }
      });   

      return false;

  } //END IF empty id

});     

This won't work until the submit button is pushed a second time (ie, $('#purchaser_contact').submit(); is not submitting the form).

EDIT This never did work for me but Simeon's answer is the closest and he claims it works in all major browsers so if it helps someone, great.

解决方案

This is the solution you are looking for.

var forceSubmitForm = false;
$('#myForm').submit(function(){
    if(forceSubmitForm) return true;
    $.ajax({
        // ...
        success: function(data){
            if(data.entry != 'OK'){ // or whatever check
                forceSubmitForm = true;
                $('#myForm').submit(); // Form will submit
            }else{
                // Form will not be submitted
            }
        }
    });
    return false;
});

EDIT To prove to myself that this methodology works, I just created a sample page with a form and a submit button. This worked fine in all major browsers:

$(function(){ // On DOM ready
    var forceSubmitForm = false;

    $('#purchaser_contact').live('submit', function() {
        if (forceSubmitForm){
            alert('submitting');
            return true;
        }

        // A callback function works in the same manner as a timeout

        setTimeout(function(){
            forceSubmitForm = true;
            $('#purchaser_contact').submit();
        }, 1500); // Submit after 1,5 seconds

        return false; // Do not submit immediately
    });
});