简体   繁体   English

Ajax - 试图替换而不是 append

[英]Ajax - trying to replace instead of append

I know nothing about ajax. I'm using flask to run a web app with python. This is regarding the response after submitting a form.我对 ajax 一无所知。我正在使用 flask 运行 web 应用程序和 python。这是关于提交表单后的响应。 All is working fine, except every time I press submit the latest response is appended under the last (until you refresh the page).一切正常,除了每次我按下提交时,最新的回复都附加在最后一个下方(直到您刷新页面)。 I have narrowed it down to only one possible section of the config based on the output. This is the section I'm confused by:我已根据 output 将其缩小到配置的一个可能部分。这是我感到困惑的部分:

    response =response.result;
                     $.each(response,function(key,val){
                     console.log(val);
                        html+="<p>"+val+"<p>"
    
                    });
                    html +="<br>";
                    $(".show-data").append(html);
                });

I noticed 'append(html)' which lead me to search some ajax functions.我注意到“append(html)”导致我搜索一些 ajax 函数。 I've tried: '$(".show-data").我试过:'$(".show-data"). replaceWith (html);'替换(html);' which I've seen work elsewhere but not here.我在其他地方看到过,但在这里没有。

Does anyone with a good understanding of Ajax have any ideas what to do here?有没有人对 Ajax 有很好的了解,有什么想法在这里做什么? Thanks in advance for any advice.在此先感谢您的任何建议。

You could change your $(".show-data").append(html);你可以改变你的$(".show-data").append(html); to $(".show-data").html(html);$(".show-data").html(html);

html() works more or less like innerHTML from standard javascript. html()的工作方式或多或少类似于标准 javascript 中的 innerHTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM