user3391137
user3391137

Reputation: 441

how to find duplicate dropdown row in a table using jquery?

There is a table which contains an user dropdown per row. Needs to prevent of selecting duplicate users of dropdown in the table.

enter image description here

While selecting the duplicate user in the above table as "easy",The js code should keep the first of user dropdowm the rest of duplicate user dropdown wants to remove from table.

HTML enter image description here

Javascript Code

function  checkDuplicateUserId(obj){
  var user_id=$("#"+obj.id+" option:selected").val();            
  $('tbody#data tr select').each(function (i, row) {
  if ($('tbody#data tr select').find('option[value="' + $(this).val() + '"]').size() > 1) {
      alert();
  } 

  });

}

function  checkAlreadySelected(obj){
    checkDuplicateUserId(obj);
    var num = parseInt($(obj).attr('num'));
    var user_id=$("#"+obj.id+" option:selected").val();            
    var next_user_id=$("#user_id_"+eval(num+1)+" option:selected").val();          
    if(user_id && typeof next_user_id == 'undefined'){
        var row = updateSrNo(num);
        $("#data").append(row);
    }

   }

As i mentioned in the js code, I have written a function as called as checkDuplicateUserId() to detect the duplicate dropdown from the table, Unfortunately the alert() not prompted while select the duplicate users.

What's the issue of jquery code which i have written wrongly there? If you have other way to accomplish this, Please say it

Thanks in advance

Upvotes: 2

Views: 1121

Answers (2)

nim_10
nim_10

Reputation: 496

Try this:

function  checkDuplicateUserId(obj){
  var user_id=$("#"  +obj.id + " option:selected").val();            

  if( $('tbody#data tr select option:selected').filter( function(){ if( $(this).val() == user_id ) return true; } ).length > 1) {
    alert('something');
  }
}

Upvotes: 1

reza.bm
reza.bm

Reputation: 171

You can do something like this:

function  checkAlreadySelected(obj){
  var user_id=$("#"+obj.id+" option:selected").val();            
  $('tbody#data tr select').each(function (i, row) {
  if ($(this).val()==user_id) {
      return false ;
  } 

  });
  return true ;
}

Upvotes: 0

Related Questions