admin管理员组

文章数量:1205047

This works well to add an option dynamically to a jQuery chosen select box;

var select = $('select', editor.field('cms_module_system_tenancies.tenant_id').node() );
var newOption = $('<option value="'+tenant_id+'" selected>'+tenant_forename+' '+tenant_surname+'</option>');
select.append(newOption);
select.trigger("chosen:updated");

But, I can't figure out how to reverse the action and remove that newly added item the next time I trigger the select list.

Is there a reverse of select.append which would remove the option from the list?

This works well to add an option dynamically to a jQuery chosen select box;

var select = $('select', editor.field('cms_module_system_tenancies.tenant_id').node() );
var newOption = $('<option value="'+tenant_id+'" selected>'+tenant_forename+' '+tenant_surname+'</option>');
select.append(newOption);
select.trigger("chosen:updated");

But, I can't figure out how to reverse the action and remove that newly added item the next time I trigger the select list.

Is there a reverse of select.append which would remove the option from the list?

Share Improve this question edited Oct 31, 2015 at 21:43 Adam Azad 11.3k5 gold badges30 silver badges72 bronze badges asked Oct 31, 2015 at 21:39 user1098178user1098178 7173 gold badges9 silver badges26 bronze badges 2
  • Well, there's remove(), which would seem to be what you mean. – David Thomas Commented Oct 31, 2015 at 21:47
  • as @DavidThomas said .remove() is what you need .. but Its better to make a DEMO cause I really can't imagine what you trying to do – Mohamed-Yousef Commented Oct 31, 2015 at 22:13
Add a comment  | 

2 Answers 2

Reset to default 20

Update jQuery Chosen Dynamically

Try this:

Remove all child nodes

$('#selectBox').empty().append('<option value="0">-- Select --</option>');  

Here we are first emptying the select box and then appending a default "Select" option.

Remove a single child node

$("#selectBox option[value='option1']").remove(); 

Trigger chosen:updated after empty() or remove()

$('#selectBox').trigger("chosen:updated"); 

Hope this is helpful.

Check prepend this will insert first. reverse to append.

本文标签: javascriptRemove option dynamically from jQuery ChosenStack Overflow