AJAX哈希提交表单

时间:2012-11-14 02:13:10

标签: php jquery ajax

我很确定它与我的core.js文件和ajax哈希网址有关。但是我正在尝试提交表单,但它并没有像我想要的那样提交。这是core.js文件:

// call init
$(init);


function init() {
    ajax_page_handler();
    page_load($(window.location).attr("hash")); // goto first page if #! is available
}

function page_load($href) {
    if($href != undefined && $href.substring(0, 2) == '#/') {
        // replace body the #content with loaded html
        $('#content').load($href.substring(2), function () {
            $('#content').hide().fadeIn('slow');
        });
    }
}

function ajax_page_handler() {
    $(window).bind('hashchange', function () {
        $href = $(window.location).attr("hash");
        page_load($href);
    });

    // this allow you to reload by clicking the same link
    $('a[href^="#/"]').live('click', function() {
        $curhref = $(window.location).attr("hash");
        $href = $(this).attr('href');
        if($curhref == $href) {
            page_load($href);
        }
    });
}

直播观看结束于www.krissales.com。表格在此处:http://www.krissales.com/#/media/5.Testing-1

点击“发表评论”链接,然后您将输入信息,然后点击评论,但它只是刷新,但不提交。

我为解决这个问题而采取的步骤是在评论文件中,在表单操作字段中,我插入了标记name="#content",因为这是我提交给我的div的名称。

原始内容在http://blog.krissales.com/article/7.Testing-3-man上(您可以在其中发布评论,并且它可以正常工作)

但显然它不起作用。你们是否知道我做错了什么?感谢您的帮助!

<script type="text/javascript">
    tinyMCE.init({
        mode : "textareas",
        theme : "simple"
    });
</script>
<form action="#/media/article.php" name="#content" method="POST">

    Name:
    <br />
    <input type="text" name="name" class="userpass"/>
    <br /><br />
    Comment:
    <br />
    <textarea id="elm1" name="comment" rows="7" cols="30" style="width: 500px;"> 
    </textarea>
    <br />
    <input type="submit" name="submit" value="Comment" class="button" />
    <input type="reset" name="submit" value="Reset" class="button" />

</form> 

4 个答案:

答案 0 :(得分:1)

我注意到你没有在'comment.php'文件上设置ajax类型。

你需要......

 $.ajax({
            type: 'POST',
        url: 'comment_ajax.php',
        data: { form_name: name, form_comment: comment },
        success: function(data) {
            $('#new_comment').prepend(data);
                        // close modal box
            // do other shit
            // kthnxbai
        }
    });

如果未指定type,则默认为GET请求,该请求不会发布数据。 :)

答案 1 :(得分:0)

您应该像这样更改表单的action属性:

<form action="script-handling-comment-data.php#/media/article.php" name="#content" method="POST">

目前,您将评论数据发送至http://www.krissales.com/,我认为主页无法处理评论发布。

答案 2 :(得分:0)

您似乎正在正确处理链接,但表单提交不是链接,您可能希望使用$(form).submit(function(){ ... })来处理提交

在您的情况下,如果您为表单提供了身份form1

$('#form1').submit(function(){
  var keyValues = $(this).serializeArray();
  var map = {};
  for(i in keyValues)
  {
    var value = keyValues.value;
    var name = keyValues.name;
    map[name] = value;
  }
  $.post($(this).attr('action'),map,function(){
    alert("Submitted values: " + $(this).serialize());
  });
  return false;
})

有关详细信息,请参阅serializeArray$.post.submit

答案 3 :(得分:0)

您当前的core.js处理URL哈希中的更改,并使用哈希重新路由任何链接以将该相对路径加载到#content。缺少的是重定向表单提交以执行相同操作的代码(将其添加到ajax_page_handler):

 $('form').live('submit', function(e) {
    var $action = $(this).attr('action');

    if($action.substring(0, 2) == '#/') {
        // replace the #content with result of the form post
        $.ajax({
            url: $action.substring(2),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            success: function(data) {
                $('#content').html(data);
                $('#content').hide().fadeIn('slow');
            }
        });
        // stop the real form submit from happening
        e.preventDefault();
    }
});