我有3个下拉列表,其中包含一个国家的地区,地点,次地点。 使用Ajax填充位置和子位置列表后,有人从第一个列表中选择一些。如何从填充Ajax的下拉列表中更改selectedIndex?
现在我的问题:
进出口使用LocalStorage
刷新页面,所以当我能保持价值和自动重新填充列表与上次选择的值。
列表已成功填写,但未能更改selectedIndex
(选择“选择城镇/选择子地址”)。
我试过每一种方法,我发现在stackoverflow上如何更改selectedIndex
,他们都没有工作,也没有人向我扔了一个错误。 所以我可以假设有一个问题ajax
? 就像js更改selectedIndex
,但ajax
调用将它更换回来,即使ajax
早于first.Could可能是这样的事情是可能的吗?
的jQuery:
jQuery(document).ready(function(){
if(localStorage) {
if(localStorage.regions) {
jQuery('#region_h').val(localStorage.regions);
alert(localStorage.regions);
getlocations_main(localStorage.regions); //ajax call for locations
}
if(localStorage.locations) {
jQuery('#location_h').val(localStorage.locations);
jQuery('#location_h option').eq(this.selectedIndex).prop('selected',true);
alert(localStorage.locations);
getsublocations_main(localStorage.locations); //ajax call for subloc
}
if(localStorage.subloc){
jQuery('#subloc_h option').eq(this.selectedIndex).prop('selected',true);
}
}
});
HTML:
<div class="row rowInput_spiti360">
<div class="ie_fix_select_spiti360" >
<div id="location_add_yo" class="mainselection" >
<select class=" searchcategory" name="location_h" id="location_h" onchange="getsublocations_main(this.options[this.selectedIndex].value)">
<option selected="selected" value="-1">Select a Town:</option>
</select>
</div>
</div>
</div>
<div class="row rowInput_spiti360">
<div class="ie_fix_select_spiti360" >
<div id="sublocation_add_yo" class="mainselection" >
<select id="subloc" name="subloc" class=" searchcategory">
<option selected="selected" value="-1">Select a sublocation:</option>
</select>
</div>
</div>
</div>
你能得到[的jsfiddle] (http://www.jsfiddle.net)? –
你为'this.selectedIndex'得到了什么?在我看来'this'指的是'window'而不是html元素。 – Arvind
在你使用的上下文中'this''应该代表文档。我认为this.selectedIndex应该是未定义的。 –