简体   繁体   English

Jquery AJAX附加数据重置

[英]Jquery AJAX appended data reset

I've this script: 我有这个脚本:

 <script>
 $(document).ready(function() {
 $('#signUpForm').submit(function() {
 $.ajax({
 type: 'POST',    
 url: "process.php",
 data: $("#signUpForm").serialize(),
 success: function(data)
 {
 $('#errors').show();
 $('#errors').append(data); 
 }
 });
 return false;    
 });
  });
 </script>

It displays errors from php script. 它显示来自php脚本的错误。 And right now every time I click submit button, new errors are added to old ones. 现在,每次单击“提交”按钮时,都会向旧版本添加新错误。 I wonder, is it possible to reset old errors and show just new, after I click submit button? 我想知道,点击提交按钮后,是否可以重置旧错误并显示新内容?

Use $('#errors').html(data); 使用$('#errors').html(data); instead of $('#errors').append(data); 而不是$('#errors').append(data); , When you use .append() , you add to the content you already had. ,当您使用.append()时 ,您将添加到您已有的内容中。 .html() replaces the content. .html()替换内容。

Another alternative might be to empty first and then append. 另一种选择可能是先清空然后追加。 Like $('#errors').empty().append(data); $('#errors').empty().append(data);

I wonder why do you have $('#errors').show(); 我想知道为什么你有$('#errors').show(); ? If it was hidden first, maybe better to put .show() after the .html() / .append()` 如果它首先被隐藏,也许最好在.html()/ .append()之后放入.show()

使用$('#errors')。empty()。append(data);

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

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