Javascript 将输入值与单个值进行比较

Javascript 将输入值与单个值进行比较,javascript,jquery,Javascript,Jquery,我正试图找出一种方法来验证被点击按钮的ID是否已经存在于一组输入中 从单击的按钮获取ID的代码 $(".detail-view button").on("click", function() { var detailID = $(this).attr('id'); 检查ID的代码不存在 function itemExists() { $("input#lunchorder_item_entry_id").each(function() { exist

我正试图找出一种方法来验证被点击按钮的ID是否已经存在于一组输入中

从单击的按钮获取ID的代码

   $(".detail-view button").on("click", function() {
       var detailID = $(this).attr('id');
检查ID的代码不存在

function itemExists() {
    $("input#lunchorder_item_entry_id").each(function() {
        existingID = $(this).val();

        if(detailID == existingID) {
           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 {

       }
    });
}

我现在讨论的问题只是部分时间有效,如果有人对更好的方法有什么建议,我相信这不是实现我目标的最佳方法。谢谢大家!

detailID
itemExists()
函数中不可见,请将其设置为全局,或将其传递给函数

itemExists(detailID){
  // if(detailID == existingID) {
}
整个过程应该是这样的

 $(".detail-view button").on("click", function() {
       var detailID = $(this).attr('id');
       itemExists(detailID);
 });
 function itemExists(detailID) {
    $("input#lunchorder_item_entry_id").each(function() {
            var existingID = $(this).val();
            if(detailID == existingID) {
               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 {

            }
     });
 }

存在范围问题,
detailID
不是代码中的全局变量。在
itemExists
函数的上下文中,它是change
$(this.attr('id')
this.id
这样,您可以保存
this
以在jQuery对象中转换,并直接访问
id
属性,而不是通过另一个函数访问它。好的,我应该提到函数
itemExists
在我的点击函数中。这并不能真正解决我的问题,因为它仍然允许事物激发其他事物。我认为我真正需要的是一种方法,将每个值与数组进行比较。