2012-12-26 128 views
1

可能重复:
How to add dynamically a combobox using jQuery创建动态组合框使用jQuery

看,我对动态comboboxs这个工作代码。

<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> 

这是做什么是创建一个新的combobox每次我填充组合框之前。

我在找什么是代码创建2组合框每次我填充组合框之前,而不是一个。

我该怎么做?

干杯

+1

考虑使用'体'和'head'标签为您的无效页面。你可以在http://jsfiddle.net上提供演示吗? – undefined

+0

@undefined http://jsfiddle.net/JaVVe/6/ – user1148875

+0

这个问题与@skdnewbie中的这个问题看起来完全一样:http://stackoverflow.com/questions/14044104/。 –

回答

0

不知道这是否你想要什么,但是这增加了两个组合框 ($( '#连击')后追加(newComboBox);):

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