所以我正在构建两个选择下拉列表,用户选择并在上面显示值。用户还可以“添加”另一个下拉菜单并从相同的选项中再次选择。
无法克隆 div 以及获取要显示的值。它似乎多次克隆 div。
HTML:
<div id="exp-display-choice">
<div class="exp-choices">
<ul class="choices">
<!-- display selctions here -->
<p>Results:</p>
</ul>
<ul class="ad-choices">
<li>
<select class="select" id="ad-type">
<option value="" selected>Choose Your Pet</option>
<option value="Cat">Cat</option>
<option value="Dog">Dog</option>
<option value="Wookie">Wookie</option>
</select>
</li>
<li>
<select class="ad-size select full-width" id="ad-size">
<option value="" selected>Choose Transportation</option>
<option value="Planes">Planes</option>
<option value="Trains">Trains</option>
<option value="Automobiles">Automobiles</option>
</select>
</li>
</ul>
</div><!-- end of exp-choices -->
</div><!-- end of exp-display-choices -->
<a href="javascript:void(0);" class="add-btn button">Add</a>
JS:
function displayAds(current_select)
{
var adChoice = current_select.val();
current_select.parents('.exp-choices').find('choices').append('<li>'+adChoice+'</li>');
}
$('.exp-choices').on('change', "option:selected", function()
{
displayAds($(this));
});
$(".add-btn").click(function()
{
// grab the last exp-choices in exp-display-choice, clone it, then append to the bottom
var $newExpChoices = $(".exp-choices").parent().children(':last').clone().appendTo($(this).parent().parent());
$newExpChoices.show().insertAfter('.exp-choices');
});