将输入值与单值进行比较

时间:2013-04-10 21:36:04

标签: 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() {
        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 {

       }
    });
}

我所拥有的这个问题只在部分时间内有效,如果有人建议更好的方法,我相信这不是达到目标的最好方法。谢谢!

1 个答案:

答案 0 :(得分:0)

detailIDitemExists()函数中不可见,要么将其设为全局,要么将其传递给函数。

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 {

            }
     });
 }
相关问题