【问题标题】:display 3rd drop down list based on 1st 2 dropdown ids根据第 2 个下拉列表显示第 3 个下拉列表
【发布时间】: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


【解决方案1】:
     $.ajax({
            type: "POST",
            url: "fetch_plots.php",
            data: {area:area, sector:sector},
            cache: false,
            beforeSend: function () { 
                $('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#output2").html( html );
            }
        });

fetch_plots.php

$area = (isset($_REQUEST["area"]) ? intval($_REQUEST["area"]) : 0);
// assuming "$area" is an integer, not a varchar
echo $area;
$sector = (isset($_REQUEST["sector"]) ? intval($_REQUEST["sector"]) : 0);
// assuming "$sector" is an integer, not a varchar
echo $sector;
if ( !empty($area) && !empty($sector) ) { 
    $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 调用返回了什么?使用您的控制台找出答案。
  • 另外,在你的 PHP 脚本中,$_REQUESTS 应该是 areasector,而不是 item_idcat_id
  • 它没有为区域和扇区取任何值
  • @user2178637 你的 fetch_plots.php 应该是这样的。你的控制台告诉你什么被发送到它?
  • 谢谢,但我是否以正确的方式传递值? &lt;select name="sector" onchange="showPlots(this);"&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2013-12-06
  • 2014-09-04
  • 2012-01-04
  • 2014-11-16
  • 2018-09-21
相关资源
最近更新 更多