javascript - 如何防止在 jquery/javascript 中向表中重复追加数据?

标签 javascript php jquery html

如何避免表中出现相同的数据?

代码如下:

var a = 0;
    $(document).on("change", "#txtInput",function(){
      var inputLength = this.value.length;
      if (inputLength <= 9) { return; }

      $("#contenido").append("<tr><td>" + this.value + "</td></tr>");
      $("p").html('Total Bianuales: ' + (++a));
    });

最佳答案

存储您已添加的值,如果输入的值已添加,请勿重新添加。

var a = 0;
var alreadyAdded = [];
$(document).on("change", "#txtInput",function(){
  var inputLength = this.value.length;
  if (inputLength <= 9) { return; }
  if ($.inArray(this.value, alreadyAdded) !== -1) { return; }

  $("#contenido").append("<tr><td>" + this.value + "</td></tr>");
  $("p").html('Total Bianuales: ' + (++a));
  alreadyAdded.push(this.value);
});

关于javascript - 如何防止在 jquery/javascript 中向表中重复追加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443011/

相关文章:

javascript - 使用 moment js 将 iso 8601 格式化为 "MMM"和 "hourPM/AM"

javascript - Meteor.js 项目和依赖管理

php - 使用 PHP 7.2.4 的 Ubuntu 16.04 上缺少 PDO 驱动程序

javascript - jQuery 重构函数

javascript - 如何使用 jquery 更改 <label> 的类?

javascript - 如何在数组中使用具有顺序但不同名称的元素?

javascript - XMLHttpRequest:网络错误 0x2efd,由于错误 00002efd,无法完成操作

javascript - 使用 Iron Router 为 Twitter 用户生成 URL

php - Mysql同一行复制同一张表120次

php - 将通过 AJAX 上传到 DIV 的变量传递到同一个 DIV 中,并用结果替换内容