所以我想将用户 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 文件
最佳答案
您正在检查名称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/