如何在ajax检查后使用onsubmit

时间:2014-03-07 09:44:32

标签: javascript php jquery ajax validation

如何在ajax检查后采用onsubmit方法? onsubmit现在不工作

$(document).ready(function(){
    $("#p_code").change(function(){
        $("#message").html("<img src='ajax_loader.gif' width='26px' height='26px' /> checking...");

        var data1 = $("#p_code").val();




    $.ajax({
        type:'POST',
        url:'check.php',

        data: $('form').serialize(),



        success: function validate(data){


            if (data==1){
                $("#message").html("<img src='tick.png' />");
            $("#div2").css("visibility","visible");
            return true

                }


            else{
                $("#message").html("<img src='cross.png' />No matches Found");
        $("#div2").css("visibility","hidden");
            return false
        }



        }
    })

    });
});

形式不起作用......

<form id="form1" name="form1" method="post" onsubmit="return validate()" action="run.php">

2 个答案:

答案 0 :(得分:0)

使用 submit()

在ajax成功之后申请$('form').submit();

$(document).ready(function () {
    $("#p_code").change(function () {

    $("#message").html("<img src='ajax_loader.gif' width='26px' height='26px' /> checking...");

    var data1 = $("#p_code").val();
    $.ajax({
        type: 'POST',
        url: 'check.php',

        data: $('form').serialize(),
        success: function validate(data) {
            if (data == 1) {
                $("#message").html("<img src='tick.png' />");
                $("#div2").css("visibility", "visible");
                $('form').submit(); //appy submit event here 

                return true

            } else {
                $("#message").html("<img src='cross.png' />No matches Found");
                $("#div2").css("visibility", "hidden");
                return false
            }



        }
    })

});

});

答案 1 :(得分:-1)

$(document).ready(function(e) {
  $(document).on('submit', '#form1', function(e) {
    e.preventDefault();
});  
});


function validate()
{

$.ajax({
    type: 'POST',
    url: "check.php",
    data: $('form').serialize(),
    success:function(data){
        if(data=='authenticated')
            {
            window.location = "run.php";
            }
    }



});
}