单击jquery中的按钮后如何添加新的选择框

时间:2016-02-21 11:47:27

标签: javascript php jquery

我不知道它是否可能使用jquery,..我有一个选择选项,有一个数据数组来自数据库,我想要的是,每当我点击一个按钮,另一个选择选项将弹出像第一个选择选项.. 这是我选择选项的代码

       <select class="form-control" name="room[]" id="room[]">
        <option value="" default>Select</option>
        <?php
        $room = $subjectsClass->room();
        foreach ($room as $key => $value) {
        echo '<option value=" ' . $value['room_id'] .' ">' . $value['room_no'] . '</option>';
        }
        ?>
        </select>
    <button type="button" class="btn btn-default" id="addRooms" >Add more Rooms?</button>
<script>

$('#addRooms').click(function(){
  //append another select box with data from database,..how??
});

</script>

1 个答案:

答案 0 :(得分:1)

让我们说你把它包装成了一个div:

<div id="the_div_of_wrapping"> all your stuff </div>

然后我会这样做:

var the_select = $("#room[]");
var the_id = the_select.prop("id");
var the_number_of_selects = $("select").length;
var the_div_of_wrapping = $("#the_div_of_wrapping");

the_select.clone().prop("id", the_id + the_number_of_selects);
the_div_of_wrapping.append(the_select);

更新

正如评论中所讨论的,我会删除id,因为它是不必要的,然后代码将是:

var the_select = $("#room[]");
var the_div_of_wrapping = $("#the_div_of_wrapping");

the_select.clone();
the_div_of_wrapping.append(the_select);