화면의 table 에 이미 있는 내용을 사용자가 또 추가입력하는 경우

(즉, 팝업을 통해서 새로운 데이타를 추가하는데,  화면에 이미 있는내용과 중복되는지 체크하는 경우)


서버를 통하지 않고, jQuery를 통해서 쉽게 방지해서 alert창을 뛰울 수 있다.






<script type="text/javascript"> function nameCheck() { var name = $('#myInput').val();

if ($('.listedName:contains(' + name + ')').length != 0) {
alert('already Exists');
return false;
}
return true;
} </script>



<table>
<tr>
<td>ID1</td>
<td class="listedName">name1</td> // name2, name3 등 반복

</tr>


.....

<form onsubmit="'return nameCheck()'">

<input id="myInput"/>


그런데, contains함수가 문제가 약간있다.

Entity단위(테이블 TD단위)로 비교를 하지 않고...    Entity안에 단어가 포함되는지를 비교하는 문제가 있다.


그래서  정확하게 비교를 하고 싶을때는 filter를 쓸 필요가 있다.  REF


if ($('.listedName').filter(function() {
return $(this).text() === name;
}).length !=0 ) {


Posted by yongary
,