【发布时间】:2015-04-06 14:05:23
【问题描述】:
我想显示一个相互关联的下拉列表。当第一个下拉列表,即选择区域列表时,相关的部门列表将填充在第二个下拉列表中。直到这个我能够做到。但是在第二个下拉菜单(即选择扇区)之后,我想显示将基于“区域”和“扇区”的图。这部分我做不到。我能够显示基于“扇区”的图,但不能同时显示区域和扇区。
这是我的代码
获取扇区
function showItems(sel) {
var cat_id = sel.options[sel.selectedIndex].value;
$("#output1").html( "" );
$("#output2").html( "" );
if (cat_id.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_sectors.php",
data: "cat_id="+cat_id,
cache: false,
beforeSend: function () {
$('#output1').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#output1").html( html );
}
});
}
}
获取地块(仅基于扇区)
function showItemDet(sel) {
var item_id = sel.options[sel.selectedIndex].value;
$("#output2").html( "" );
if (item_id.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_plot.php",
data: "item_id="+item_id,
cache: false,
beforeSend: function () {
$('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#output2").html( html );
}
});
}
}
我该怎么做?
***编辑****
我做了这样的事情
<script>
function showPlots(area, sector) {
var item_id = sel.options[sel.selectedIndex].value;
var cat_id = sel.options[sel.selectedIndex].value;
$("#output2").html( "" );
if (item_id.length > 0 ) {
$.ajax({
type: "POST",
url: "fetch_plot.php",
data: {area: item_id, sector:cat_id},
cache: false,
beforeSend: function () {
$('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#output2").html( html );
}
});
}
}
</script>
在 fetch_plots 中
$area = ($_REQUEST["area"] <> "") ? trim( addslashes($_REQUEST["area"])) : "";
echo $area;
$sector = ($_REQUEST["sector"] <> "") ? trim( addslashes($_REQUEST["sector"])) : "";
if ($item_id <> "" && $cat_id<>"") {
$sql = "SELECT * FROM plots where area_id=".$area." AND sec_id=".$sector."";
echo $sql;
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<select name="plot">
<option value="">Select Plot</option>
<?php while ($rs = mysqli_fetch_array($query)) { ?>
<option value="<?php echo $rs["plot_id"]; ?>"><?php echo $rs["name"]; ?></option>
<?php } ?>
</select>
<?php
}
}
?>
【问题讨论】:
-
在这种情况下传递“区域”和“扇区”。第二个ajax中的两个ID
-
我不知道如何在 ajax 或 jquery 中传递 2 个值
-
使用
curlys创建要传递的值列表。 -
看到这个答案是正确的
-
请问您为什么不使用
$,get快捷方式?您可以在global AJAX settings中设置beforeSend,而不必为每次通话重复。
标签: javascript php jquery