jQuery populate dynamic created select box from another select

心不动则不痛 提交于 2020-01-03 05:26:08

问题


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/


回答1:


$(".cyclePoSelect:last").html($(".cyclePoSelect:first").html())



回答2:


$(".cyclePoSelect:last").html($(".cyclePoSelect:eq(0)").html());

Reference:

  • :last
  • :eq


来源:https://stackoverflow.com/questions/27449569/jquery-populate-dynamic-created-select-box-from-another-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!