jquery - Knokout select : Set Selected Item by item name not value -
i have knockout select following
observable array
issuingcountries
0: objectcoordinatorregion: "eu" country: "australia" countryid: 14 id: 1 2: objectcoordinatorregion: "au" country: "japan" countryid: 16 id: 2 html
<select class="issuing_country" data-bind="options: issuingcountries, optionstext : 'country', value:issuingcountryselected, optionscaption:'---select---', " > </select> my question , have { country: "japan" } in hand,so how set particular selected item selected in dropdown.? far have tried
$('.issuing_country option[text="japan"]').prop('selected', true); but failed ,let me know anyother way this.
you shouldn't trying set using jquery when using knockout - instead, on viewmodel.
your <select> options bound issuingcountries observablearray, , value selected bound issuingcountryselected observable. select option automatically, set property on viewmodel, , knockout take care of rest:
//assuming "japan" @ index 2: vm.issuingcountryselected(vm.issuingcountries()[2]); if know name of option want select, first must find right item, you'd need function that:
function findcountrybyname(name) { (var x = 0; x < vm.issuingcountries().length; x++) { if (vm.issuingcountries()[x].country == name) return vm.issuingcountries()[x]; } return null; } vm.issuingcountryselected(findcountrybyname("japan"));
Comments
Post a Comment