aiternal
aiternal

Reputation: 1100

Selecting selectbox options by array values

I am getting some values via json type ajax request with Jquery. When I use alert(msg.options) it alerts ["1","3","8"]

If I use below script to select needed options, it works. It selects only options that have value 1 and 3 and 8 :

$('#input_6').val(["1","3","8"]);

But below script doesn't work even if it alerts the same:

$('#input_6').val(msg.options);

How can I fix this? Thank you...

Upvotes: 1

Views: 268

Answers (2)

Andrew Jackman
Andrew Jackman

Reputation: 13966

$(document).ready(function () {
    var msg = {};
    msg.options = '["1","3","8"]'
    $('#input_6').val( eval( msg.options ) );
});

http://jsfiddle.net/yjHUS/2/

Upvotes: 0

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

Try:

$('#input_6').val($.parseJSON(msg.options));

Upvotes: 4

Related Questions