的Html如何處理不同的動態組合框?
<div id="opts" class="styled-select">
<select id="combo1" class="combo" data-index="1">
<option></option>
<option val="Opt1">Opt1</option>
<option val="Opt2">Opt2</option>
<option val="Opt3">Opt3</option>
</select>
</div>
<div id="opts2" class="styled-select">
<select id="combo2" class="combo2" data-index="1">
<option></option>
<option val="Opt11">Opt11</option>
<option val="Opt22">Opt22</option>
<option val="Opt32">Opt33</option>
</select>
</div>
jQuery的
$('#opts').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'), 10);
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[val="' + selectedValue + '"]').remove();
$('#opts').append(newComboBox);
}
}
});
我不能複製/使用2個不同的組合框相同的代碼,它似乎引起某種麻煩。
如何爲2個組合框具有相同的「效果」?
乾杯。
您是否嘗試過使用'類= 「二合一」'你的第二個選擇? –
是的,但沒有工作。你也可以在這裏嘗試一下:http://jsfiddle.net/JaVVe/6/ – pleaseDeleteMe
那麼你想要達到什麼目的?我看到的元素被克隆和追加 –