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 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含