Rumah > pembangunan bahagian belakang > tutorial php > ajax提交表单为什么页面还跳转呢?

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

WBOY
Lepaskan: 2016-06-06 20:09:23
asal
1203 orang telah melayarinya

数据能提交也能返回。但是页面会跳转, 跳转后的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>
Salin selepas log masuk
Salin selepas log masuk
<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>
Salin selepas log masuk
Salin selepas log masuk

回复内容:

数据能提交也能返回。但是页面会跳转, 跳转后的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>
Salin selepas log masuk
Salin selepas log masuk
<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>
Salin selepas log masuk
Salin selepas log masuk

因为你提交的按钮是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>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Artikel sebelumnya:Laravel 5.2 Zizaco/entrust 无法生存数据库 Artikel seterusnya:CodeIgniter多模块配置问题
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Isu terkini
Topik-topik yang berkaitan
Lagi>
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan