Heim >Backend-Entwicklung >PHP-Tutorial >ajax提交表单为什么页面还跳转呢?

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:09:231227Durchsuche

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

回复内容:

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

因为你提交的按钮是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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Laravel 5.2 Zizaco/entrust 无法生存数据库Nächster Artikel:CodeIgniter多模块配置问题

In Verbindung stehende Artikel

Mehr sehen