gpt4 book ai didi

javascript - 使用 jquery 创建动态组合框

转载 作者:行者123 更新时间:2023-11-28 09:28:14 26 4
gpt4 key购买 nike

Possible Duplicate:
How to add dynamically a combobox using jQuery

看,我有动态组合框的工作代码。

<html>
<div id="combos" class="styled-select">
<select id="combo1" class="combo" data-index="1">
<option></option>
<option>sss</option>
<option>aaa</option>
<option>ddd</option>
</select>
</div>

<script>
$('#combos').on('change', '.combo', function () {
var selectedValue = $(this).val();

if ($(this).find('option').size() > 2) {
var newComboBox = $(this).clone();
var thisComboBoxIndex = parseInt($(this).attr('data-index'), 2);
var newComboBoxIndex = thisComboBoxIndex + 1;
$('.parentCombo' + thisComboBoxIndex).remove();
if (selectedValue !== '')
{
newComboBox.attr('data-index', newComboBoxIndex);
newComboBox.attr('id', 'combo' + newComboBoxIndex);
newComboBox.addClass('parentCombo' + thisComboBoxIndex);
newComboBox.find('option[value="' + selectedValue + '"]').remove();
$('#combos').append(newComboBox);
}
}
});
</script>
</html>

它的作用是在我之前每次填充组合框时创建一个新的组合框。

我正在寻找的是,每次我之前填充组合框时,代码都会创建 2 个组合框,而不是一个。

我怎样才能做到这一点?

干杯

最佳答案

不确定这是否是您想要的,但这会添加两个组合框(在 $('#combos').append(newComboBox); 之后:

var otherComboBox = $('#combo' + newComboBoxIndex).clone();
otherComboBox.attr('id', 'othercombo' + newComboBoxIndex);
$('body').append(otherComboBox);

关于javascript - 使用 jquery 创建动态组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14040045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com