jQuery从另一个选择中填充动态创建的选择框 [英] jQuery populate dynamic created select box from another select
问题描述
我有一个表单,您可以通过jQuery动态添加新行。它创造的元素很好。但是,我在新行上有一个选择框。当它被创建时,它是空的。我需要从该类型(类)的第一个选择框中填充它。
这是HTML ...
< select class =cyclePoSelectname =poNumber []style =WIDTH:100px>
< option value =0selected =>选择一个< / option>
< option value =VE-13475> VE-13475< / option>
< option value =VZ-61300> VZ-61300< / option>
< / select>
< option value =0selected =>选择一个< / option>
< option value =VE-13475> VE-13475< / option>
< option value =VZ-61300> VZ-61300< / option>
< / select>
<! - 这是动态创建的元素 - >
< select class =cyclePoSelectname =poNumber []style =WIDTH:100px>
< option value =0selected =>选择一个< / option>
< / select>
我需要添加从第一个select到最后一个select的2个选项。添加它们应该没有问题。但我似乎无法从第一个选择框中获得选项列表。到目前为止,我已经尝试过......
$(。cyclePoSelect> option:first)。each(function( ){
alert(this.text +''+ this.value);
});
这只是给我选择一个选项。
$(.cyclePoSelect> option)。each(function(){
alert(this.text +''+ this.value);
});
这给了我所有的选择,但是从两个现有的选择框中。
jsFiddle - http://jsfiddle.net/scott80109 / tkok9vwy /
$(。cyclePoSelect:last) .html($(.cyclePoSelect:first)。html())
I have a form where you can dynamically add new rows via jQuery. It creates the elements fine. However, I have a select box on the new row. When it is created, it is empty. I need to populate it from the first select box of that type (class).
Here is the HTML...
<select class="cyclePoSelect" name="poNumber[]" style="WIDTH: 100px">
<option value="0" selected="">Select One</option>
<option value="VE-13475">VE-13475</option>
<option value="VZ-61300">VZ-61300</option>
</select>
<select class="cyclePoSelect" name="poNumber[]" style="WIDTH: 100px">
<option value="0" selected="">Select One</option>
<option value="VE-13475">VE-13475</option>
<option value="VZ-61300">VZ-61300</option>
</select>
<!-- This is the dynamically created element -->
<select class="cyclePoSelect" name="poNumber[]" style="WIDTH: 100px">
<option value="0" selected="">Select One</option>
</select>
I need to add the 2 options from the first select to the last select. Adding them should be no problem. But I can't seem to get a list of options from just the first select box. So far, I have tried...
$(".cyclePoSelect > option:first").each(function() {
alert(this.text + ' ' + this.value);
});
That just gives me the "Select One" option.
$(".cyclePoSelect > option").each(function() {
alert(this.text + ' ' + this.value);
});
That gives me all options, but from BOTH existing select boxes.
jsFiddle - http://jsfiddle.net/scott80109/tkok9vwy/
$(".cyclePoSelect:last").html($(".cyclePoSelect:first").html())
这篇关于jQuery从另一个选择中填充动态创建的选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!