Javascript JQuery比较数组中的任何匹配项
我有一个很简单的问题,我想,但我无法解决它 在表单提交中,我想比较两个隐藏输入类型的值,如果发现任何匹配项,则向用户返回警报并阻止提交。几乎所有隐藏的输入类型值都是1-3,可能是1、12、123、13等等。因此,如果是1和123,则抛出警报 所以我试过这样的方法,但是我很明显对我在做什么感到困惑,呵呵Javascript JQuery比较数组中的任何匹配项,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个很简单的问题,我想,但我无法解决它 在表单提交中,我想比较两个隐藏输入类型的值,如果发现任何匹配项,则向用户返回警报并阻止提交。几乎所有隐藏的输入类型值都是1-3,可能是1、12、123、13等等。因此,如果是1和123,则抛出警报 所以我试过这样的方法,但是我很明显对我在做什么感到困惑,呵呵 var new_products = $('#new_products'); var array_new_products = jQuery.makeArray(new_products);
var new_products = $('#new_products');
var array_new_products = jQuery.makeArray(new_products);
var existing_products = $('#existing_products');
var array_existing_products = jQuery.makeArray(existing_products);
$("#my_form").submit(function(e) {
if (jQuery.inArray(existing_products, new_products) >= 0) {
e.preventDefault();
alert ("This Promotion matches one or more products already associated to this Group. If you continue the existing Promotion will be cancelled and replaced with the currently selected Promotion!");
}
return true;
});
我愿意通过比较字符串和返回匹配项或其他任何方式来实现这一点。我只是刚刚接触Jquery。提前谢谢
$.each($('#new_products').val().split(''), function(i, char) {
var existing = $('#existing_products').val();
if (existing.indexOf(char) != -1)
alert('mathces found');
});
检查从
#新产品
返回的值中是否有任何字符存在于从#现有产品
返回的值中?您不能调用.split()
的结果中的.each()
。你是说.forEach()
?或者你是想用$来代替每个?很漂亮,非常感谢。现在像冠军一样工作。从美国东部时间下午2点开始我就一直在玩它:(.+1用于更正哈哈。我想为什么…不会…这个…有效?!?!?!