比较输入值与单一值
问题描述:
我试图找出一种方法来验证一个按钮被点击的ID,并不存在于一组输入中。从按钮获取ID比较输入值与单一值
代码点击的检查ID
$(".detail-view button").on("click", function() {
var detailID = $(this).attr('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 {
}
});
}
+0
这并不能真正解决我的问题,因为它仍然允许其他事情发生。我想我真正需要的是一种将每个值与数组进行比较的方法。 – philecker 2013-04-11 13:13:50
有一个范围的问题,'detailID'是不是在你的代码中的全局变量。在'itemExists'函数的上下文中,通过这种方式将''this(this).attr('id');''转换为'this.id' – undefined 2013-04-10 21:37:57
并将其转换为jQuery对象直接访问'id'属性,而不是通过一个函数访问它。 – fernandosavio 2013-04-10 22:10:01
好吧,我应该提到'itemExists'函数在我的点击函数中。 – philecker 2013-04-10 22:22:46