加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – AJAX哈希提交表单

发布时间:2020-12-13 22:46:09 所属栏目:PHP教程 来源:网络整理
导读:我很确定它与我的core.js文件和ajax哈希网址有关.但是我正在尝试提交表单,但它并没有像我想要的那样提交.这是core.js文件: // call init$(init);function init() { ajax_page_handler(); page_load($(window.location).attr("hash")); // goto first page i
我很确定它与我的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

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

我用来解决它的步骤是在注释文件中,在表单操作字段中,我插入了标记名称=“#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>

解决方法

您当前的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();
    }
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读