PS: There were two answer before, and I refresh this page, the answer disappeared, what’s going on?
demo: demo in jsbin
html
<table>
<tr class='test_tr'>
<td>
<select class='test_select'>
<option value=1>test1</option>
<option value=2>test2</option>
<option value=3>test3</option>
</select>
</td>
<td><a href="#" class='test_clone'>clone</a></td>
</tr>
js
$(document).ready(function(){
$('select.test_select').selectmenu();
$('.test_clone').click(function(){
var tp = $(this).parents('.test_tr');
var new_tr = tp.clone(true);
new_tr.insertAfter(tp);
tr_func(new_tr);
})
});
function tr_func(new_tr){
$('.test_select',new_tr).selectmenu();
}
After click the clone button and click on the new select, it always affect to the first one.
Any suggestion? Thanks!
This problem has a few interesting aspects:
When a row is cloned, all items with an
idattribute will be cloned as well, causing two elements with the same#ID; that’s not good. It can be solved by creating a pristine sample row that gets cloned every time you click the button which you then have to “decorate” before it can be used (i.e. apply.selectmenu()and click handler).When a
<select>is cloned, it doesn’t retain the selected option. You have to save theselectedIndexproperty and apply it to the cloned version.Both problems solved looks like this:
Demo