How To Update Select Options Before Showing Them In Jquery?
Look at this example code:
Solution 1:
I'm a little bit confused.. do you just want to update the select when the page loads? If so you can do:
CSS:
#teste {
display: none;
}
JS:
$(function(){
$('#teste').append('<option id="pera">pera</option>').val("pera").show();
});
Solution 2:
I think this might be what you are searching for:
Using the value:
$('[name=options]').val( 3 );//To select Blue
To reset it use:
$('[name=options]').val( '' );
Using the text:
$('[name=options] option').filter(function() {
return ($(this).text() == 'Blue'); //To select Blue
}).prop('selected', true);
Link: http://forum.jquery.com/topic/how-to-dynamically-select-option-in-dropdown-menu
Solution 3:
i think you want to prevent showing multiple times the teste option.
<head><scripttype="text/javascript"src="http://code.jquery.com/jquery-1.10.2.min.js"></script><style>#teste{
display: none;
}
</style><scripttype="text/javascript">
$(document).ready(function(){
$(document).click(
function(){
if(!$('#teste').is(':visible')){
$('<option/>',{
'id' : 'pera',
'html' : 'pera',
'selected' : 'selected'
}).appendTo('#teste');
$('#teste').show('slow');
}
})
});
</script></head><body><selectid="teste"><optionvalue="banana">banana</option><optionvalue="laranja">laranja</option></select></body>
Post a Comment for "How To Update Select Options Before Showing Them In Jquery?"