jQuery Mobile表单提交结果为“未定义”提交页面

时间:2013-03-23 17:23:33

标签: html5 jquery css3 jquery-mobile

我有一个表单,我希望使用jQuery Mobile提交。但是,当您提交表单时,页面将更改为提交页面并显示“未定义”。页面上没有其他内容。

这是jquery / ajax:

$("#submit_comment").click(function() {

            var formData = $("#comment_form").serialize();

            $.ajax({
                type: "POST",
                url: "forms/comment_form.php",
                cache: false,
                data: formData,
                success: onSuccess,
                error: onError          

            });

            return false;

        });

这是HTML表单:

<form id="comment_form" action="forms/comment_form.php" method="POST" style="display:none";>
                    <input type="hidden" name="user_id" value="<?php echo $session_user_id; ?>">
                    <textarea id="text_area_input" name="comment" placeholder="Enter a comment here."></textarea>
                    <input type="submit" id="submit_comment" name="submit_comment" value="Post Comment">

                </form>

这是提交页面:

if (empty($_POST['submit_comment']) === false) {
    if (empty($_POST['user_id']) === true) {
        $comment_errors[] = 'Sorry, there was an error submitting your comment. Please try again.';
    }
    if (empty($_POST['comment']) === true) {
        $comment_errors[] = 'You must enter something into the comment field.'; 
    } 
    if (empty($comment_errors) === false) { 
        echo "<div id='comment_errors'><?php echo output_comment_errors($comment_errors);?></div>";
    } else if (empty($comment_errors) === true) {
        $comment = $_POST['comment'];
        $user_id = $_POST['user_id'];       
        echo "ok";
    }
}

1 个答案:

答案 0 :(得分:2)

工作示例:http://jsfiddle.net/Gajotres/B8mrX/

HTML:

<form id="comment_form">
        <input type="hidden" name="user_id" value=""/>
        <textarea id="text_area_input" name="comment" placeholder="Enter a comment here."></textarea>
        <input type="submit" id="submit_comment" name="submit_comment" value="Post Comment"/>
 </form>

JS:

$('#comment_form').on('submit', function(e){       
    e.preventDefault();

    var formData = $("#comment_form").serialize();

    $.ajax({
        type: "POST",
        url: "forms/comment_form.php",
        cache: false,
        data: formData,
        success: function (result) {
            alert('Success');
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }        

    });    

    return false;
});

如您所见,我已删除了表单属性方法和操作。

我使用表单提交事件而不是按钮点击事件。使用 e.preventDefault(); 返回false 阻止提交事件,并且休息在ajax上。我在ajax调用中添加了成功和错误回调函数,但你可以按照自己的意愿去做。

编辑:

这是一个工作示例,只需将其置于新的HTMl中并进行测试即可。我在jsFiddle示例中忘记了两件事。首先提交事件需要在一个正确的jQuery Mobile页面事件内(没有它提交事件将不会绑定到表单)我们需要通过添加数据关闭经典的jQuery Mobile表单提交-ajax =&#34; false&#34 ;属性来形成标签。

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>      
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
    <script>
        $(document).on('pagebeforeshow', '#index', function(){      
            $('#comment_form').on('submit', function(e){

                e.preventDefault();

                var formData = $("#comment_form").serialize();

                $.ajax({
                    type: "POST",
                    url: "forms/comment_form.php",
                    cache: false,
                    data: formData,
                    success: function (result) {
                        alert('Success');
                    },
                    error: function (request,error) {
                        alert('Network error has occurred please try again!');
                    }        

                });    

                return false;
            }); 
        });         
    </script>
</head>
<body>
    <div data-role="page" id="index">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
            <a href="#second" class="ui-btn-right">Next</a>
        </div>

        <div data-role="content">
            <form id="comment_form" data-ajax="false">
                    <input type="hidden" name="user_id" value=""/>
                    <textarea id="text_area_input" name="comment" placeholder="Enter a comment here."></textarea>
                    <input type="submit" id="submit_comment" name="submit_comment" value="Post Comment"/>
             </form>
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>    
</body>
</html>