[英]how to set a default value in a dynamic depandant drop down list
我使用了一个jQuery脚本和一些php代码来生成两个选择菜单。 一个菜单包含状态,其他菜单包含。 更改状态菜单后,将加载对应的区域。 我的问题是如何设置“所有城市”的默认值。 该脚本会自动加载在mysql表中找到的区域,因此我无法找到一种方法来执行此操作。 我想我必须更改脚本中的某些内容。 任何想法都会有所帮助。
$(document).ready(function(){
function populate() {
if($('#states').val() == 'AK' || $('#states').val() == 'DC') // Alaska and District Columbia have no counties
{
$('#county_drop_down').hide();
$('#no_county_drop_down').show();
} else {
fetch.doPost('../getCounties.php');
}
}
$('#state').change(populate);
var fetch = function() {
var counties = $('#search_area');
return {
doPost: function(src) {
$('#loading_county_drop_down').show(); // Show the Loading...
$('#county_drop_down').hide(); // Hide the drop down
$('#no_county_drop_down').hide(); // Hide the "no counties" message (if it's the case)
if (src) $.post(src, { state_code: $('#state').val() }, this.getCounties);
else throw new Error('No SRC was passed to getCounties!');
},
getCounties: function(results) {
if (!results) return;
counties.html(results);
$('#loading_county_drop_down').hide(); // Hide the Loading...
$('#county_drop_down').show(); // Show the drop down
}
}
}();
populate();
});
您没有提及“所有城市”是否在您的数据库中,所以我假设它不在。 另外,我不确定您要确切返回什么,因此我将假定县为选择输入。
未经测试的答案,但也许会有所帮助。
返回县列表后,您可以在列表前添加“全部”选项,然后选择它...接下来要做的唯一另一件事就是确保在服务器上处理此表单的任何人都知道该做什么。使用“全部”。
getCounties: function(results) {
if (!results) return;
var allCities = $("<option value=\"All\">All Cities</option>");
counties.html(results);
counties.prepend(allCities);
counties.val("All").attr('selected',true);
$('#loading_county_drop_down').hide(); // Hide the Loading...
$('#county_drop_down').show(); // Show the drop down
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.