??yy 2012-09-04 17:11 采纳率: 0%
浏览 23

jQuery动态下拉菜单

I have the following code select box div:

Make:<br> 
        <select name="Make">
        <option value = "Toyota">Toyota</option>
        <option value = "Honda">Honda</option>
    </select>


<div id="side_model">           
        Model:<br> 
        <select name="Model">
        <PHP CODE THAT SEARCHES DATABASE FOR ALL MODELS AVAILABLE AND LIST THEM AS OPTIONS ?>
    </select>
</div>

And the following Jquery:

<script>
jQuery(document).ready(function($){
    $('#Make').change(function() {
        $.get('dropdown.php',{make: $(this).val() },function(d){
            $('#side_model').html(d);
        });
    });
});
</script>

This is a very simplified version of my code but what Im doing is, once the make is chosen I query the database in dropwdown php and get all models of that make and then simply populate the list

My dropdown.php looks something like this:

Model:<br> 
            <select name="Model">
            <PHP CODE THAT SEARCHES DATABASE FOR ALL MODELS THAT MATCH THE MAKE SELLECTED AND LIST THEM AS OPTIONS ?>
        </select>

Ok so the code pretty much works the way its intended too except for one detail.

Instead of the jQuery replacing the div with the contentes of dropdown.php it is just adding that extra select box on top of it.

How do I get the jQuery code to replace the model select box with the dynamicaly genereated content?

  • 写回答

3条回答 默认 最新

  • weixin_33736048 2012-09-04 17:23
    关注

    Maybe clear the div before you refill it?

    $('#side_model').empty();
    

    and fill it again with

    $('#side_model').append(/*your generated html code*/);
    

    --EDIT--

    I also noticed you call $('#make'), but your select only has a name like that, so please add id="make"to your select

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题