javascript - How select default option of dynamically added dropdown list -
i string server using jquery:
<option value='1'>1</option><option value='2'>2</option> <option value='3'>3</option><option value='4'>4</option>
then create dropdown within div:
$.get('server.php', function(data) { $('#mydiv').html('<select id="mysel" name="mysel">'+data+'</select>'); });
problem when create this, want select 1 option so:
$("#mysel").val("3");
but doesn't work! if add alert before calling line, works!
alert('test'); $("#mysel").val("3");
can not find problem.
note: can not change way data server.
assuming line $("#mysel").val("3");
outside of $.get() {}
...
the response $.get
asynchronous $("#mysel").val("3");
executes before html <option>
elements added.
adding alert()
delays execution long enough <option>
exist in scenario.
the simplest solution here move $("#mysel").val("3");
inside $.get() {}
body.
Comments
Post a Comment