0

I have 30 users with id 1~30
And the memberInThisGroup is an array [2,24]
And I want to check if the id is in the array(2,24) then give it the option with selected

string +="<option value=\""+data.userId+"\"  selected >"+data.userName+"</option>"; 

else , just a normal option

string +="<option value=\""+data.userId+"\">"+data.userName+"</option>";    

Here is My code with error ,It seems like it just check one time,and the selected option is opposite
Please guide me how to write
thank you

function getCurrentusers(memberInThisGroup,groupId) {
    console.log(memberInThisGroup);  //2,24
    $.ajax({
        type : 'GET',
        url : 'currentusers',
        async : true,
        success : function(datas) {  
                //for loop every elements
                var string ="";
                for(var i in datas){
                    var data = datas[i];    
                    if(memberInThisGroup.indexOf(data.userId)){
                        console.log("We have member"+data.userId);
                        string +="<option value=\""+data.userId+"\"  selected >"+data.userName+"</option>"; 
                    }else{
                        console.log("We don't have member"+data.userId);
                        string +="<option value=\""+data.userId+"\">"+data.userName+"</option>";    
                    }
                }
                $("#e_"+groupId).append(string);    
                $('#e_'+groupId).multiSelect('refresh');            

        }
    });
}

datas ajax get is json like:

[{"userId":1,"userName":"Ken","password":"a12345","account":"[email protected]"},
{"userId":2,"userName":"wen","password":"qqqq","account":"[email protected]"},
{"userId":3,"userName":"win","password":"1234","account":"[email protected]"},...]

Here is my console output:

We have member1
We don't have member2
We have member3
We have member4
We have member5
.... 

But what I expect is :

We have member2
We have member24
We don't have member1
We don't have member3
We don't have member4
We don't have member5
....
2
  • what is the error given? Commented Jun 6, 2015 at 11:00
  • Note that your code requires a shim for Array.prototype.indexOf to work in IE8 and below (or use this ES5 shim: github.com/es-shims/es5-shim) Commented Jun 6, 2015 at 11:13

1 Answer 1

1

Try this

var string = "",
    memberInThisGroup = [2, 24], // just for test 
    data, selected;

for (var i = 0, len = datas.length; i < len; i++) {
    data     = datas[i];
    selected = (memberInThisGroup.indexOf(data.userId) >= 0) ? 'selected' : '';

    string += "<option value=\"" + data.userId + "\" " + selected + ">" + data.userName + "</option>"; 
}

Example

Sign up to request clarification or add additional context in comments.

2 Comments

Thank youso much!! It works well.By the way,do you know where is the error in my logic??
1. don't use for..in for Arrays. 2. indexOf returns position or -1 (if element does not exists in array) you need check result >= 0 or !== -1,

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.