Home > Backend Development > PHP Tutorial > ajax提交表单为什么页面还跳转呢?

ajax提交表单为什么页面还跳转呢?

WBOY
Release: 2016-06-06 20:09:23
Original
1204 people have browsed it

数据能提交也能返回。但是页面会跳转, 跳转后的url是get形式的。

如:http://127.0.0.1:8080/MyBlog/Admin/Blog/insert?title=111&tags=111

<code><script type="text/javascript">
    $('#submit').on('click', function () {
        var action = $('#form1').attr('action');
        var title = $('#title').val();
        var tags = $('#tags').val();
        var content = $('#textarea1').val();
        $.post(
                action,
                {title: title, tags: tags, content: content},
                function (data) {
                    layer.msg(data.message, {
                        icon: data.icon,
                        time: 2000 //2秒关闭(如果不配置,默认是3秒)
                    });
                }
        );
    });
</script></code>
Copy after login
Copy after login
<code> <div class="main-content">
        <div class="container">
            <div class="xb4">
                <h1 class="float-left text-black margin-large-bottom">写博客</h1>
                <form id="form1" action="%7B:U('Admin/Blog/insert')%7D" class="margin-large-top">
                    <div class="form-group">
                        <div class="field">
                            <input type="text" class="input" id="title" name="title" size="30" placeholder="标题">
                        </div>
                        <div class="field margin-big-top">
                            <input type="text" class="input" id="tags" name="tags" size="30" placeholder="关键字。多个关键字用,分开">
                        </div>
                    </div>
            </form>
</div>
            <div class="xb10">
                        <textarea name="content" id="textarea1" style="height:400px;max-height:500px;">
                            <p></p>
                        </textarea>
                <button id="submit" class="button border-black float-left margin-big-top">发表</button>
            </div>
            
        </div>
<pre class="brush:php;toolbar:false"><code>        if(M('article')->add($add)){
            $data['message'] = '发布成功';
            $data['icon'] = 6;
            $this->ajaxReturn($data);
        }else{
            $data['message'] = '发布失败';
            $data['icon'] = 5;
            $this->ajaxReturn($data);
        }</code>
Copy after login
Copy after login

回复内容:

数据能提交也能返回。但是页面会跳转, 跳转后的url是get形式的。

如:http://127.0.0.1:8080/MyBlog/Admin/Blog/insert?title=111&tags=111

<code><script type="text/javascript">
    $('#submit').on('click', function () {
        var action = $('#form1').attr('action');
        var title = $('#title').val();
        var tags = $('#tags').val();
        var content = $('#textarea1').val();
        $.post(
                action,
                {title: title, tags: tags, content: content},
                function (data) {
                    layer.msg(data.message, {
                        icon: data.icon,
                        time: 2000 //2秒关闭(如果不配置,默认是3秒)
                    });
                }
        );
    });
</script></code>
Copy after login
Copy after login
<code> <div class="main-content">
        <div class="container">
            <div class="xb4">
                <h1 class="float-left text-black margin-large-bottom">写博客</h1>
                <form id="form1" action="%7B:U('Admin/Blog/insert')%7D" class="margin-large-top">
                    <div class="form-group">
                        <div class="field">
                            <input type="text" class="input" id="title" name="title" size="30" placeholder="标题">
                        </div>
                        <div class="field margin-big-top">
                            <input type="text" class="input" id="tags" name="tags" size="30" placeholder="关键字。多个关键字用,分开">
                        </div>
                    </div>
            </form>
</div>
            <div class="xb10">
                        <textarea name="content" id="textarea1" style="height:400px;max-height:500px;">
                            <p></p>
                        </textarea>
                <button id="submit" class="button border-black float-left margin-big-top">发表</button>
            </div>
            
        </div>
<pre class="brush:php;toolbar:false"><code>        if(M('article')->add($add)){
            $data['message'] = '发布成功';
            $data['icon'] = 6;
            $this->ajaxReturn($data);
        }else{
            $data['message'] = '发布失败';
            $data['icon'] = 5;
            $this->ajaxReturn($data);
        }</code>
Copy after login
Copy after login

因为你提交的按钮是button,button的type默认是submit,当你点击按钮时,先触发ajax,接着执行form表单提交

那你得手动阻止跳转:

<code class="javascript">$('#submit').on('click', function(e) {
    e.stopPropagation();
    e.preventDefault();
    var action = $('#form1').attr('action');
    var title = $('#title').val();
    var tags = $('#tags').val();
    var content = $('#textarea1').val();
    $.post(
        action,
        {title: title, tags: tags, content: content},
        function(data) {
            layer.msg(data.message, {
                icon: data.icon,
                time: 2000 //2秒关闭(如果不配置,默认是3秒)
            });
        }
    );
});</code>
Copy after login
Related labels:
source:php.cn
Previous article:Laravel 5.2 Zizaco/entrust 无法生存数据库 Next article:CodeIgniter多模块配置问题
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template