jquery - 在jquery中设置自动填充选择框的选定值

标签 jquery set drop-down-menu

我的页面上有 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'>&raquo;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/

相关文章:

php - 加速 jQuery AutoComplete(不可避免的长列表)

jquery - 如何判断滚动 Pane 在 JavaScript 中滚动的方式?

javascript - 列表是否包含具有特定值的项目?

jquery 自动完成列表不坚持父输入元素

java - HashSet.remove() 未能按照规范运行?

set - 基本 Tcl -- 打印变量值

python - 为什么在 python 中从列表中创建一个 freezeset 会转换列表?

ios - 表格 View 中的下拉列表

java - 如何在 Mule 连接器中添加下拉列表项?

java - Swing - Android 类似下拉菜单