doudao8283 2015-08-11 09:27
浏览 51
已采纳

jQuery 3下拉菜单 - onChange

I have three populated drop down menus. Each drop down menu is populated based on the choice of the previous selection. This means that the choices of "selector 2" are based on the choices of "selector 1", while the choices of "selector 3" are based on the choices of "selector 2". The drop down menu is populated dynamically using jquery, and the source is from a MySQL table.

When I select "selector 1", it populates "selector 2" and when I select "selector 2" it populates "selector 3". This works fine. The problem lies that if I have "selector 1", "selector 2" and "selector 3" selected, and I change "selector 1", the value of "selector 2" is lost (how it should be), but the value of "selector 3" is remaining there. I want the selector value of 3 to be reset when "selector 1" is changed and not retain the old value of the previous old selector.

This is my jquery

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>

    function getTertiary(val) {
        $.ajax({
            type: "POST",
            url: "get_tertiary.php",
            data:'tertiary_cat='+val,
            success: function(data){
                $("#tertiary_cat").html(data);
            }
        });
    }


    function getSecondary(val) {
        $.ajax({
            type: "POST",
            url: "get_secondary.php",
            data:'primary_cat='+val,
            success: function(data){
                $("#secondary_cat").html(data);
            }
        });
    }

    function selectPrimary(val) {
        $("#search-box").val(val);
        $("#suggesstion-box").hide();
    }
</script>

This is my html:

<div class="frmDronpDown">
    <div class="row">
        <label>Primary:</label><br/>
        <select name="primary_cat" id="primary_cat" class="demoInputBox" onChange="getSecondary(this.value);">
            <option value="">Select Primary</option>
            <?php
            foreach($results as $primaryCat) {
                ?>
                <option value="<?php echo $primaryCat["primary_cat"]; ?>"><?php echo $primaryCat["primary_cat"]; ?></option>
                <?php
            }
            ?>
        </select>
    </div>
    <div class="row">
        <label>Tertiary:</label><br/>
        <select name="secondary_cat" id="secondary_cat" class="demoInputBox" onChange="getTertiary(this.value);">
            <option value="">Select Seconary</option>
        </select>
    </div>
    <div class="row">
        <label>State:</label><br/>
        <select name="tertiary_cat" id="tertiary_cat" class="demoInputBox">
            <option value="">Select Tertiary</option>
        </select>
        <input type="text"/>
    </div>
</div>

When I tried - to change

<select name="primary_cat" id="primary_cat" class="demoInputBox" onChange="getSecondary(this.value);">

to:

<select name="primary_cat" id="primary_cat" class="demoInputBox" onChange="getSecondary(this.value);getTertiary(this.value);">

I am getting this error in error_log

PHP Warning:  Invalid argument supplied for foreach()
  • 写回答

3条回答 默认 最新

  • dsfs64664 2015-08-11 09:39
    关注

    Reset the value of the third DDown on Re stetting the second one like this :

    function getSecondary(val) {
        $.ajax({
            type: "POST",
            url: "get_secondary.php",
            data:'primary_cat='+val,
            success: function(data){
                $("#secondary_cat").html(data);
                 $("#tertiary_cat").html('<option value="">Select Tertiary</option>')
            }
        });
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳