开发者

jQuery - Remove select list values

开发者 https://www.devze.com 2023-02-05 20:31 出处:网络
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:

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

精彩评论

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

关注公众号