My jquerymobile form has a optgrouped multi section dropdown. Upon form loading I have to set some default options. I have tried like
$('#myselect')[0].selectedIndex = 2;
$('#myselect')[0].selectedIndex = 3;
$('#myselect')[0].selectedIndex = 5;
$('#myselect').selectmenu("refresh");
But only 5th option is getting selected, how to set selected option true for multiple options using js.
+++ EDIT ++++
<!DOCTYPE html>
<link rel="stylesheet" href="jquery.mobile-1.0b3/jquery.mobile-1.0b3.min.css" />
<script src="jquery.mobile-1.0b3/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$("#account").live("pagecreate", function(event){
if( navigator.userAgent.match(/android/i) ) {
loadOrgDropdown();
}
});
</script>
<script src="jquery.mobile-1.0b3/jquery.mobile-1.0b3.min.js"></script>
<script src="js/common.js"></script>
<script type="text/javascript">
function loadDropdown() {
jQuery.ajax(
{
url : 'PocketmediServices.php?service=getDropd开发者_运维百科ownValues',
type : 'get',
dataType : 'json',
beforeSend : function(){
},
error : function(){
if (typeof Android != 'undefined') {
Android.dismissLoadinDialog();
}
},
success : function(data){
if(data.response.getDropdownValues.dropdownList !== undefined) {
var count = -1;
var selected = -1;
$.each(data.response.getDropdownValues.dropdownList, function(key, val) {
count++;
$('#myselect')
.append($('<option>', { value : val.item.Code })
.text(val.item.Name));
if($.inArray(val.item.optionval, previousSelected) >=0) {
$('#myselect')[0].selectedIndex = [3,5]; // hard coded for now.
}
});
$('#myselect').selectmenu("refresh");
}
},
complete : function(req){
},
});
} //loadDropdown
</script>
<div data-role="fieldcontain">
<label for="organization">Organization:</label>
<select name="organization" id="organization" data-native-menu="false" multiple >
<optgroup label="s" id="org_s"></optgroup>
<optgroup label="Support Networks" id="org_support_networks"></optgroup>
</select>
</div>
Thanks, nehatha
Multiple select boxes deals with array for multiple values e.g.
$('#myselect').val([2,3,5]);
You can find examples @ http://api.jquery.com/val/
精彩评论