jQuery - 根据ajax响应停止表单提交,否则让它提交

时间:2017-06-06 17:45:15

标签: javascript jquery ajax

我设置了一些jQuery代码,以便当用户尝试提交此表单时,它会检查来自php中的'check_bid_locked'函数的ajax响应是什么。如果它被“分配”,“过期”或“是”,那么它使用e.prevent_default()来停止提交并显示模态弹出消息(这是有效的)。

我似乎无法弄清楚如果响应与其中任何一个都不匹配,如何让表单被提交。基本上,如果回答为“否”,那么我们希望提交表单。有什么想法吗?

HTML

    <form onsubmit="return true check_submits();" method="post" action="<?php echo get_permalink($pid); ?>"> 
   <input type="hidden" name="control_id" value="<?php echo base64_encode($pid); ?>" /> 
   <input class="submit_bottom" id="submits_crt" type="submit" name="bid_now_reverse" value="<?php _e("Place Bid",'BidTheme'); ?>" />
  </form>

Jquery的

$(document).ready("#bid_now_reverse").submit(function(e) {

var allowSubmit = false;
if (allowSubmit == false) {
e.preventDefault();
}

    var data = {
        'action': 'check_bid_locked',
        'pid': <?php echo $pid; ?>,
        'uid': <?php echo $uid; ?>
    };

    jQuery.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', data, 
function(response) {
            //If bid NOT locked
            if(response == 'no') {
            allowSubmit == true;
            }
            //If Assigned
            if(response == 'assigned') {
            confirm('<div class="bid_panel_box_title">Task Has Been 
Assigned</div><div class="padd10_center">Your offer can\'t be deleted.
</div>', {
                    /* 
                    Define your buttons here, can handle multiple buttons 
                    with custom title and values
                    */
                    buttons: [
                        { class: "delete_button15", type: "button", title: 
"Close", value: "Cancel" }

                    ],
                    modal: true
                }, 
                //Reload page START
                function(resp) {
                   console.log(resp);
            if(resp == 'Cancel'){
                window.location.reload(true);
            } } 
            //Reload page END
        );                  
        //If Expired
    }else if(response == 'expired'){
        confirm('<div class="bid_panel_box_title">Task Has Expired</div><div class="padd10_center">Your offer can\'t be deleted.</div>', {
                /* 
                Define your buttons here, can handle multiple buttons 
                with custom title and values
                */
                buttons: [
                    { class: "delete_button15", type: "button", title: "Close", value: "Cancel" }

                ],
                modal: true
            }, 
            //Reload page START
            function(resp) {
               console.log(resp);
        if(resp == 'Cancel'){
            window.location.reload(true);
        } } 
            //Reload page END
        );
        //If Change Offer Locked
    }else if(response == 'yes'){
    allowSubmit = false;
        confirm('<div class="bid_panel_box_title">Offer Change Locked</div><div class="padd10_center">The Poster is reviewing your offer.<br/><br/>Check back shortly to see if it was accepted or to change it.</div>', {
                /* 
                Define your buttons here, can handle multiple buttons 
                with custom title and values
                */
                buttons: [
                    { class: "delete_button15", type: "button", title: "Close", value: "Cancel"}

                ],
                modal: true
            }, 
            //Reload page START
            function(resp) {
               console.log(resp);
        if(resp == 'Cancel'){
            window.location.reload(true);
        } } 
            //Reload page END
        );
        } 
});

});

3 个答案:

答案 0 :(得分:-1)

使用submit

if(response == 'no'){
    $('form').submit();
}

答案 1 :(得分:-1)

因为有问题,这里是check_submits函数。它基本上只是在提交表单时使用,以确保用户不会尝试提交空白条目,检查长度等。

 <script type="text/javascript">
   function check_submits()
{

if(jQuery("#days_done").length > 0)
if( jQuery("#days_done").val() == '' ) 
{
    alert("<?php _e('Please type in the number of days.','ProjectTheme'); ?>");
    return false;   
}

if( jQuery("#bid").val() == '' ) 
{
    alert("<?php _e('Please type in a bid value.','ProjectTheme'); ?>");
    return false;   
}

else if( jQuery("#bid").val().length > 4 ) 
{
    alert("Offer Amount should not be more than 4 digits");
    return false;   
}

return true;
}



</script>

答案 2 :(得分:-1)

根据目前为止的建议,这是我已将代码更改为的内容,但如果ajax响应为“不”,则仍然无法提交。

HTML

<form id="submit_offer"  method="post" action="<?php echo get_permalink($pid); ?>"> 
<input class="submit_bottom" id="submits_crt" type="submit" name="bid_now_reverse" value="<?php _e("Place Bid",'ProjectTheme'); ?>" />
</form>

JQUERY

$(document).ready("#bid_now_reverse").submit(function(e) {

    var data = {
        'action': 'check_bid_locked',
        'pid': <?php echo $pid; ?>,
        'uid': <?php echo $uid; ?>
    };

    e.preventDefault();

        jQuery.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', data, function(response) {
            //If offer NOT locked then allow form to submit
            if(response == 'no'){
            $('form').submit();
            //If Assigned
            }else if(response == 'assigned') {
            confirm('<div class="bid_panel_box_title">Task Has Been Assigned</div><div class="padd10_center">Your offer can\'t be deleted.</div>', {
                    /* 
                    Define your buttons here, can handle multiple buttons 
                    with custom title and values
                    */
                    buttons: [
                        { class: "delete_button15", type: "button", title: "Close", value: "Cancel" }

                    ],
                    modal: true
                }, 
                //Reload page START
                function(resp) {
                   console.log(resp);
            if(resp == 'Cancel'){
                window.location.reload(true);
            } } 
                //Reload page END
            );              
            //If Expired
        }else if(response == 'expired'){
            confirm('<div class="bid_panel_box_title">Task Has Expired</div><div class="padd10_center">Your offer can\'t be deleted.</div>', {
                    /* 
                    Define your buttons here, can handle multiple buttons 
                    with custom title and values
                    */
                    buttons: [
                        { class: "delete_button15", type: "button", title: "Close", value: "Cancel" }

                    ],
                    modal: true
                }, 
                //Reload page START
                function(resp) {
                   console.log(resp);
            if(resp == 'Cancel'){
                window.location.reload(true);
            } } 
                //Reload page END
            );
            //If Change Offer Locked
        }else if(response == 'yes'){
            confirm('<div class="bid_panel_box_title">Offer Change Locked</div><div class="padd10_center">The Poster is reviewing your offer.<br/><br/>Check back shortly to see if it was accepted or to change it.</div>', {
                    /* 
                    Define your buttons here, can handle multiple buttons 
                    with custom title and values
                    */
                    buttons: [
                        { class: "delete_button15", type: "button", title: "Close", value: "Cancel"}

                    ],
                    modal: true
                }, 
                //Reload page START
                function(resp) {
                   console.log(resp);
            if(resp == 'Cancel'){
                window.location.reload(true);
            } } 
                //Reload page END
            );
            }       
    });
});