开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜