javascript - jQuery clone select list remove selected - Stack Overflow

I have a button that is cloning an element on the page each time the user clicks it. It is duplicating

I have a button that is cloning an element on the page each time the user clicks it. It is duplicating a select list for them to choose another option.

However, its cloning the whole element including the option that was selected so all of the new ones appended have a default value which I dont want.

 globalLocales = $("<div />").append($('[name=localeID]:first').clone()).html();
 $('select').select2();

Is there a way I can remove the selected option during the cloning process so it doesn't carry over to the new element?

I tried using .removeProp('selected') in the append as well as .prop('selected',false); but that didn't work for me

I have a button that is cloning an element on the page each time the user clicks it. It is duplicating a select list for them to choose another option.

However, its cloning the whole element including the option that was selected so all of the new ones appended have a default value which I dont want.

 globalLocales = $("<div />").append($('[name=localeID]:first').clone()).html();
 $('select').select2();

Is there a way I can remove the selected option during the cloning process so it doesn't carry over to the new element?

I tried using .removeProp('selected') in the append as well as .prop('selected',false); but that didn't work for me

Share Improve this question asked Nov 11, 2014 at 15:38 SBBSBB 8,99035 gold badges118 silver badges235 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

One way to fix the proble is to select a nonexistent value:

$("<div />").append($('[name=localeID]:first').clone().val(-1)).html();

Or you can find selected option and remove selected attribute:

$("<div />").append($('[name=localeID]:first').clone()
                    .find(':selected').removeAttr('selected').end()).html();

but this is a little clumsy.

you can remove the selected attribute with this code.

$('[name=localeID] option:selected').removeAttr('selected');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744781453a4593367.html

相关推荐

  • javascript - jQuery clone select list remove selected - Stack Overflow

    I have a button that is cloning an element on the page each time the user clicks it. It is duplicating

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信