javascript - 使用 jQuery 将 HTML 变量与 CSV 项目进行比较

标签 javascript jquery csv

所以我想将用户 checkout 的项目与 csv 文件进行比较。 checkout 项目来自 div 类 item-options,我希望能够查看它,然后检查名称是否在 csv 文件中。目前,脚本无法检测用户的购物篮中是否有正确的商品。

example, the item is in the csv file

HTML

 <div class="item-options">
             <!--START: itemnamelink--><a href="product.asp?itemid=[ITEM_CATALOGID]">[ITEM_NAME]</a><!--END: itemnamelink-->
             <!--START: itemnamenolink--><span id="Span1">[ITEM_NAME]</span><!--END: itemnamenolink-->
             <!--START: itemoptions-->
             <br />
             <a href="#" onclick="toggleProdOptions('opt[ITEM_ID]')">View/Hide options</a><br />
             <div id="opt[ITEM_ID]" name="opt[ITEM_ID]" style="display:none;">[OPTIONS]</div>
             <!--END: itemoptions-->
             <!--START: recurring_frequency-->
             <br />
             This item will Autoship every <strong>[recurring_frequency]</strong>
             <!--END: recurring_frequency--></div>
         </div>

脚本

$(document).ready(function() {

 $.ajax({
  type: "GET",
  url: "assets/exports/pipe.csv",

  success: function(data) {

    var itemToStore = document.getElementsByClassName("item-options");

    for (var i = 0; i < itemToStore.length; i++) {
      var name = itemToStore [i].innerText;
      console.log("Name: " + name);

    // Remove \n and split by ,
    var pipeList = data.split('\r\n').map(function(row){return row.split(',')});
    // Array of arrays like be generated
      for(var i = 0; i < pipeList.length; i++){
          if ($.inArray(name, pipeList[i]) != -1) {          

              console.log('value is Array!');
              }
          else {
            console.log("it is not");
          }
      }

    }
  }
});

});

CSV 文件

CSV file

console.log(pipeList)

最佳答案

您正在检查名称1 1/2"3m lenghts - White Class D

1 1/2""3m 长度 - 白色 D 级"

这显然返回 false。

if ($.inArray(name, pipeList[i]) != -1) { .. }

同时在 $.ajax 选项中添加 dataType: "text"

$.ajax({
  type: "GET",
  url: "assets/exports/pipe.csv",
  dataType: "text"

关于javascript - 使用 jQuery 将 HTML 变量与 CSV 项目进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50254459/

相关文章:

javascript - 将 html5 Canvas 输出捕获为视频或 swf 或 png 序列?

javascript - .each() 绑定(bind)悬停事件并将变量传递给处理函数

javascript - 如果单击按钮,如何清除我的输入,同时如果输入为空,如何使按钮不可单击?

javascript - d3.js 树 - 节点的 CSV 属性

cakephp - 使用数组实现 Cakephp 模型

Javascript读取txt文件中的特定行并为其分配一个id

javascript - 单击更改多个 div 的 css 样式?

javascript - 在javascript中获取对象的值

python - 无法从 csv 文件中获取前 N 个重复出现的值

javascript - 为什么我的公共(public)文件夹无法在 NodeJS 应用程序中获取 '/' URL 的文件?