0

我认为我有一个非常简单的问题,但我无法解决。

在表单提交时,我想比较两种隐藏输入类型的值,如果找到任何匹配项,则向用户返回警报并阻止提交。几乎隐藏的输入类型值将是 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 还很陌生。提前致谢。

4

1 回答 1

2
$.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

于 2013-05-03T21:10:10.727 回答