1

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...

3
  • Please show the JSON :-p Commented Jan 25, 2011 at 11:41
  • You have to parse the JSON first. msg.options seems to be a string. If it would be an array, alert would show 1,3,8. @Falcon's answer is probably right (despite typo), no clue why he deleted it. But to be certain about this you would have to show the JSON and how you handle the response. Commented Jan 25, 2011 at 11:43
  • Oh and I just read that you are using dataType: json for the Ajax request. Then it seems you are building the JSON not correctly (or at least not suitable) for your situation. Commented Jan 25, 2011 at 11:50

2 Answers 2

3

Try:

$('#input_6').val($.parseJSON(msg.options));
Sign up to request clarification or add additional context in comments.

8 Comments

This is the correct answer. The fact that '["1","3","8"]' was alerted instead of '1,3,8' means that msg.options is not an object but a string.
Great. Thank you very much @fd . It works. I guessed the problem something about Json but couldn't find what it is. Thanks. :)
+1 for the correct approach.. by the way you can't get notifications using @ as the minimum are 3 letters - are you aware of this?
@Ahmet Kemal: Check how build the JSON. You can avoid the extra parsing if you build it correctly. This would really solve the problem and not just compensate it.
@Shadow Wizard no I dont know what it is. Whatelse can I use to mention fd? @Felix King I think there is no problem about Json. It works properly now. Thanks.
|
0

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

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

1 Comment

@Sydnam thank you for help. First answer works and also simple.

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.