2

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

2
  • Put all your code in a JS Fiddle and share the link. That way it'd be much easier because we need your HTML code(not image) Commented Aug 21, 2016 at 4:53
  • It would be easier for the user to understand if you disable the selections rather than remove them. It will probably be easier for you to implement as well. Commented Aug 21, 2016 at 6:22

2 Answers 2

1

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');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.