我有一个函数,我试图检查一个值是否已经存在。然而,即使值存在,它仍然返回 -1。我试图让我的 if/else 语句工作,如果一个项目存在它“警报”,如果它不运行函数 addItem();
$(".detail-view button").on("click", function () {
itemExists();
function itemExists() {
var detailID = $(this).attr('id');
var itemArr = [];
$("input#lunchorder_item_entry_id").each(function (index) {
var lunchItemID = $(this).val();
itemArr.push(lunchItemID);
});
addItem();
alert(jQuery.inArray(detailID, itemArr));
/*
if (jQuery.inArray(detailID, itemArr)) {
alert("You have already added this item. If you want to change item details, please remove the item and re-add it to your cart. Thank You!");
} else {
// the element is not in the array
addItem();
} */
console.log(itemArr);
}