dongzhansong5785 2012-02-22 12:43
浏览 29
已采纳

如何根据php和jquery中其他下拉列表的值来填充下拉列表?

I have one drop down which is populated from database and on the same page I have a seconde drop down which will be populated from the value of the 1st one.

I want to use ajax for this so what should I do for this. And tutorial or a link from where I can solve my problem.

Updated: Here is the code which I used.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

    $('#cat').onChange(function(){
    alert("oncha");
        if($('#cat').val() == 'uprofile'){
            file = 'uprofile.html';
        }else if ($('#cat').val() == 'other'){

            file = 'city.html';
        }
        $('#city').change(function(){
            loadusers = $.ajax({
                    type: "GET",
                    url: file,
                    cache: false,
                    success: function (html) {
                        $("#city").html(html);
                    }
            });
        });
    });
</script>

<tr>
           <td class="label"><span id="required_span">* </span>Category</td>
           <td colspan="2" class="data"><select name="cat" id="cat">
           <option value="0">--Select Category--</option>

           <option value="O/O">O/O</option>
           <option value="company driver">company driver</option>
           <option value="other">other</option>

           </select></td>
      </tr>

 <tr>
           <td class="label"><span id="required_span">* </span>City</td>
           <td colspan="2" class="data"><select name="city" id="city">
           <option value="0">--Select city--</option>



           </select></td>
      </tr>

Here is the content of the city.html

<option>1</option>
<option>2</option>

But cannot work even the alert function. What I am missing in the code?

  • 写回答

2条回答 默认 最新

  • dswm97353 2012-02-22 13:09
    关注

    You can use something like this code:

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
        $(function() {
            if($('select').val() == 'uprofile'){
                file = 'uprofile.html';
            }else if ($('select').val() == 'album'){
    
                file = 'album.html';
            }
            $('#dropdown').change(function(){
                loadusers = $.ajax({
                        type: "GET",
                        url: file,
                        cache: false,
                        success: function (html) {
                            $("#dropdown2").html(html);
                        }
                });
            });
        });
    </script>
    <body>
        <div id="edit-type-wrapper">
         <select id="dropdown">
           <option value="">Select one</option>
           <option value="albums">Album</option>
           <option value="uprofile">User Profile</option>
         </select>
         <select id="dropdown2">
         </select>
        </div>
    </body>
    

    Obviously you need to return the data from the other file/files. Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀