2013-05-03 3 views
0

나는 꽤 간단한 문제가 있다고 생각하지만 해결할 수는 없습니다.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를 처음 접했어. 미리 감사드립니다.

답변

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에서 반환 된 값에 존재하는지?

+2

'.split()'의 결과에'.each()'를 호출 할 수 없습니다. .forEach()를 의미 했습니까? 아니면 대신'$ .each'를 사용 했습니까? – Ian

+0

@ 이안 - 나는 $을 의미했습니다. 각자,주의 해 주셔서 감사합니다! – adeneo

+0

아름답고, 대단히 감사합니다. 이제는 챔피언처럼 일합니다. 나는 오후 2시 (동부 표준시) 이후로 그걸로 장난을 쳤다 : (. +1 보정을 위해 하하. 나는 왜 ... 실마리 ...이 ... 일을하고 있었나?!?!?! – kyle

관련 문제