Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php AJAX表单提交仅在第一次提交时有效_Php_Jquery_Ajax_Ajaxform - Fatal编程技术网

Php AJAX表单提交仅在第一次提交时有效

Php AJAX表单提交仅在第一次提交时有效,php,jquery,ajax,ajaxform,Php,Jquery,Ajax,Ajaxform,SCRIPT.JS $(document).ready(function(){ $('form').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', url: 'data/addtext.php', dataType: 'HTML', success: function

SCRIPT.JS

$(document).ready(function(){
    $('form').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: 'data/addtext.php',
            dataType: 'HTML',
            success: function (result) {
                $('#meo').html(result);
            }
        });
    });
});
INDEX.PHP

<div id="meo"></div>
现在,我想再次打印出来,所以我会再次单击提交按钮,但在第二次提交时,没有打印出来。为什么?

我想要的输出是

HELLO WORLD HELLO WORLD
换句话说,我想在每次提交时打印“HELLO WORLD”,但旧的输出将保留。因此,如果我提交表单3次,那么输出应该是

HELLO WORLD HELLO WORLD HELLO WORLD
我知道我可以使用append和不使用form来完成这项工作,但我想使用formajax来完成这项工作,而不需要重新加载页面。同样,此代码正在工作,但仅在第一次单击submit时有效


为什么它在第二次单击提交时不起作用?

您没有将结果附加到
#meo
您每次都完全替换它

而不是

$('#meo').html(结果)

尝试将
result
附加到
#meo
中的现有值/html。比如:


$('meo').html($('meo').text()+结果)

您没有将结果附加到
#meo
您每次都完全替换它

而不是

$('#meo').html(结果)

尝试将
result
附加到
#meo
中的现有值/html。比如:


$('meo').html($('meo').text()+结果)

。html()
覆盖您设置的元素内的内容。试试
。改为append()

。html()
覆盖您设置的元素内的内容。试试
。改为append()

您没有将结果附加到
#meo
中,而是每次都完全替换它。而不是
$('#meo').html(结果)尝试
$('meo').html($('meo').text()+结果)
@ochi,因为你的评论,我终于得到了答案,答案很简单,我没有注意到。感谢您没有将结果附加到
#meo
中,您每次都会完全替换它。而不是
$('#meo').html(结果)尝试
$('meo').html($('meo').text()+结果)
@ochi,因为你的评论,我终于得到了答案,答案很简单,我没有注意到。谢谢你,谢谢!我没有意识到每次提交时我都会替换它。这很有效,谢谢!我没有意识到每次提交时都要替换它。这也很有效。谢谢一旦我的声望达到15,我会提高投票率。这也很有效。谢谢我一到15岁就要投票了。
$(document).ready(function(){
    $('form').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: 'data/addtext.php',
            dataType: 'HTML',
            success: function (result) {
                // This should work for you
                $('#meo').html($('#meo').html() + result);
            }
        });
    });
});
HELLO WORLD HELLO WORLD HELLO WORLD
$(document).ready(function(){
    $('form').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: 'data/addtext.php',
            dataType: 'HTML',
            success: function (result) {
                // This should work for you
                $('#meo').html($('#meo').html() + result);
            }
        });
    });
});