这是我的 URL:www.xyz.com/index?city=NY 我正在尝试根据 url 的城市值更新 <option> 标记的 'selected' 属性。这是我的部分 html:
<script>
function loadCity(val)
{
window.location.assign("/do/index?city="+val); //script that reload's page as per selected city value.
}
$(document).ready ( function(){
var cityValue= <?=$_GET['city'] ?>;
var k = getElementById(cityValue);
k.option.setAttribute('selected', true);
}); //this function not working
</script>
<select id="city" onchange="loadCity(this.value)">
<option>Cities</option>
<?php $cities=get_cities(); ?> //get cities from an array
<?php foreach($cities as $city): ?>
<option id="<?= $city?>" value="<?= $city?>"><?= $city?></option>
<?php endforeach; ?>
</select>
尝试了一堆其他的东西,没有任何接缝可以工作。提前致谢。(非常感谢您的回复!)