问题
I have a <select> list, which has been populated with several options, but want to remove those options to start again.
I am using jQuery and have tried the following:
$("#selectId").length = 0;
But this seems to have no effect.
Part of my problem is that I am using Firebug to debug the JavaScript, but the debugger does not break at the breakpoint, so I cannot see what is going on. Should it break when the JavaScript is within the <head> of my HTML file?
回答1:
this would do:
$('#selectId').html('');
回答2:
Just $("#selectId option").remove(); works too.
回答3:
$("#selectId").empty(); works for me.
The $("#selectId option").remove(); removed the select from the DOM.
Currently using jQuery JavaScript Library v1.7.1
回答4:
The fastest way to accomplish this:
$("#selectId").empty();
Here are some tests: http://jsperf.com/find-remove-vs-empty
I take no credit for these tests. See this stack overflow question: How do you remove all the options of a select box and then add one option and select it with jQuery?
回答5:
If without jquery:
javascript SELECT remove()
回答6:
$("#selectId").options.length = 0;
That won't actually work as written because it's using a jQuery selector. It'd work if you used a straight DOM getElementById on the select list, though. Either AKX's or d.'s responses will set you right.
回答7:
Try This for Multiple Select Drop Down.
$('#FeatureId').multiselect("deselectAll", false).multiselect("refresh");
来源:https://stackoverflow.com/questions/1104662/jquery-select-list-removes-all-options