我的jquery ajax表单提交无效

时间:2013-08-06 02:35:36

标签: php jquery ajax forms submit

我有以下javascript使用ajax和jquery提交表单。

$(function() {
    $("#signform").submit(function() {
        alert();
        $.ajax({
            type: "POST",
            url: "page/sign.php",
            data: { mail: $("#inputEmail").val(), 
                    pass: $("#inputPassword").val(),
                    nick: $("#inputNick").val(),    
                    date: $.datepicker.formatDate('yy/mm/dd', new Date()) },
            sucsess: handler,
            error: function(err) { alert('error: ' + err.status) }
        });
    });

    function handler(var){
        $('#message').html(val);
    }
});

和html代码在这里,使用bootstrap!

<form class="form-horizontal" id="signform">

// ... something codes

  <div class="form-group">
    <div class="col-offset-2 col-lg-10" align="center">
      **<button type="submit" class="btn btn-default">Sign in</button>** &nbsp;
      <a href="index.php"><button type="button" class="btn">Cancel</button></a>
    </div>
  </div>

</form>

当我按下提交按钮时,它会转到索引页面。

我不知道出了什么问题。

js代码与sign.php

在同一页面中

2 个答案:

答案 0 :(得分:2)

您没有阻止提交按钮的默认操作,您可以调用event.preventDefault(),或者从提交处理程序返回false来修复此

同样在handler方法中,参数被称为var无效

$(function() {

    $(document).on('submit', "#signform", function(e) {
        $.ajax({
            type: "POST",
            url: "page/sign.php",
            data: { 
                mail: $("#inputEmail").val(), 
                pass: $("#inputPassword").val(),
                nick: $("#inputNick").val(),    
                date: $.datepicker.formatDate('yy/mm/dd', new Date()) 
            },
            success: handler,
            error: function(err) { 
                alert('error: ' + err.status) 
            }

        });
        return false; //this will prevent the default action and will stop the event propagation
        //if you do not want to stop event propagation you can use
        //e.preventDefault();
    });

    function handler(val){ //also invalid variable name var here
        $('#message').html(val);

    }
});

答案 1 :(得分:0)

试试这个,它会对你有所帮助,这里U =页面网址,F =页面方法,D =数据

U = sign.php,

F =页,

D =“mail:'+ $(”#inputEmail“)。val()+',pass:'+ $(”#inputPassword“)。val()+',nick:'+ $(”# inputNick“)。val()+',date:'+ $ .datepicker.formatDate('yy / mm / dd',new Date())+'”

// function for call page method using ajax


     function Operation(U, F, D) {
        $.ajax({
            type: "POST",
            url: U + '/' + F,
            data: D,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            cache: false,
            success: function (r) {
                var str = r.d;
        });
    }
相关问题