Form not submitting inside $.ajax success function

前端 未结 11 1704
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 20:34

I\'m validating for duplicate names by using jquery+Ajax. Everything is working fine except that the form is not submitting once everything returns true

What

11条回答
  •  醉酒成梦
    2021-01-18 21:26

    I would check it with jQuery validator at run time, not on submit but another approach is to make a Rest Style call. I think it's not necessary to POST a full form just to check 1 field.

    $('#form1').submit(function(){
        //Check not empty
        if(!$('#shelf_name').val()) {
           alert('Shelf name is required');
           $('#shelf_name').focus();
        } else {   
           //Valiate Rest style call             
           $.getJSON("check-duplicate-shelf-name.php/".concat($('#shelf_name').val()), function(data) {
               //If you only have 2 states I would return boolean to faster check ex: if(!data){
               if(data == 'stop') {
                   // working if duplicate name is found
                   alert('Shelf name already exists'); 
               } else {   
                   alert('else working?'); // alert box is showing up if name is not duplicate                                         
                   $('#form1').submit(); // but after alert, this line not executing
               }
           });
        }
        return false;
    });​
    

    When I said on comments that you can post your form manually not using $(this).submit(); I refer to:

    $.ajax({
           url: "./myurl",
           cache: false,
           type: "POST",           
           data: $("#form1").serialize(),
           success:  function(){
                   console.log("Submit successful");
       }
    });
    

提交回复
热议问题