繁体   English   中英

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

[英]how to add new select box after clicking a button in 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> 

假设您已将其包装到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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM