我的页面上有 2 个名为 [Municipality] 和 [brgy] 的选择框,但 [brgy] 选择是由用户使用 jquery 中的change() 事件在 [Municipality] 选择框中选择的内容自动填充的。以下是我的代码..
这是用于填充 brgy 选择框的市政选择框
$("select[name='Municipality']").change(function(){
// get the selected option value of country
var optionValue1=$(this).val();
$("#Dbrgy")
.html('ajaxLoader.gif')
.load('all.php', {Muni:true, MunVal: optionValue1, status: 1}, function(response) {
if(response) {
$("#Dbrgy").css('display', '');
} else {
$("#Dbrgy").css('display', 'none');
}
});
});
这是处理请求的 all.php
<?if(isset($_POST['Muni']))
{
$value = $_POST['MunVal'];
$q=mysql_query("SELECT * FROM `tbl_brgy` WHERE `brgy_id`='$value'");
?>
<select name="brgy" id="brgy"class="input" style='width:150px;'>
<option value='0'>»SELECT</option>
<?while($result=mysql_fetch_array($q)){?>
<option value="<?php echo $result['brgy#'] ?>"><?php echo $result['name'] ?>
</option><?}?>
</select>
<?}?>
现在填充 brgy 选择框后,我需要设置从 mysql 数据库检索的选定值。
这就是我从数据库设置所选值的方式
if(isset($_POST['RetrieveUpdate']))
{
$qure=mysql_query("SELECT * FROM `tbl_touristspot` where `spot_id`=$_POST[Id]") or die (mysql_error());
while($result=mysql_fetch_array($qure))
{
echo"<script> $('#brgy').val($result[brgy]);</script>";
}
但这似乎不起作用。
最佳答案
我已经解决了这个问题...自动填充选择框的问题是您必须首先为第一个设置值,然后第二个将相应地填充第一个的值...我发现当我设置时间时延迟,它实际上设置了所有所需的值,我用我的算法解决了这个问题
>set value to the first <select> and trigger it to change($('#select').val('values here').trigger('change');)
>then make some delay (settimeout(function(){},700);)
>then set the value of the auto populated <select>change($('#select').val('values here').trigger('change');)
效果很好=)
关于jquery - 在jquery中设置自动填充选择框的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967801/