jQuery - Remove select list values
I开发者_运维知识库 have some jQuery code that checks the current page title and adds it as the selected value to a select list. The code is:
function displayVals() {
var pgTitle = $('h1').val();
}
$('select').change(displayVals);
displayVals();
Is there a way to remove the other values from the select list that are not selected?
$(document).ready(function() {
if (!$("select option:selected"))
$(this).remove();
});
that it?
Try something like
$("#sel1").change(function(){
var options = $(this).find("option");
var selectedOpt = options.filter(":selected");
options.remove();
$(this).append(selectedOpt);
});
See a working demo
精彩评论