JQuery比较任何匹配数组
问题描述:
我有一个非常简单的问题,但我不能解决它。JQuery比较任何匹配数组
表单提交我想比较两个隐藏输入类型的值,如果发现任何匹配,则向用户返回警报并阻止提交。几乎隐藏的输入类型值将是1-3,可能是1,12,123,13等。所以如果1和123,发出警报。
所以我尝试过这样的事情,但我显然对我在做什么感到困惑。
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');
});
检查是否有任何从#new_product
返回值的字符在从#existing_products
返回的值存在?
你不能对'.split()'的结果调用'.each()'。你的意思是'.forEach()'?还是你的意思是使用'$ .each'来代替? – Ian 2013-05-03 21:31:56
@Ian - 我的意思是$。每个,感谢您的注意! – adeneo 2013-05-03 21:35:41
美丽,非常感谢你。现在像冠军一样工作。我一直在使用它,因为像下午2点东部时间:(。+1为更正哈哈。我就像为什么......不会......这个......工作?!?!?! – kyle 2013-05-03 21:40:48