gpt4 book ai didi

jquery - 如何在选择下拉列表中隐藏所选选项

转载 作者:行者123 更新时间:2023-11-28 07:25:28 27 4
gpt4 key购买 nike

<ul><li>option1</li><li>option2</li><ul>

我正在使用 ul li 来选择下拉菜单。已选择的值不应出现在下拉列表中。我在 jsfiddle 中有代码片段

jsfiddle:http://jsfiddle.net/v363gkj6/

对此的任何支持。

最佳答案

你可能想试试这个:

// Iterate over each select element
$('select').each(function() {

// Cache the number of options
var $this = $(this),
numberOfOptions = $(this).children('option').length;

// Hides the select element
// $this.addClass('s-hidden');

// Wrap the select element in a div
$this.wrap('<div class="select"></div>');

// Insert a styled div to sit over the top of the hidden select element
$this.after('<div class="styledSelect"></div>');

// Cache the styled div
var $styledSelect = $this.next('div.styledSelect');

// Show the first select option in the styled div
$styledSelect.text($this.children('option').eq(0).text());

// Insert an unordered list after the styled div and also cache the list
var $list = $('<ul />', {
'class': 'options'
}).insertAfter($styledSelect);

// Insert a list item into the unordered list for each select option
for (var i = 0; i < numberOfOptions; i++) {
$('<li />', {
text: $this.children('option').eq(i).text(),
rel: $this.children('option').eq(i).val()
}).appendTo($list);
}

// Cache the list items
var $listItems = $list.children('li');

// Show the unordered list when the styled div is clicked (also hides it if the div is clicked again)
$styledSelect.click(function(e) {
e.stopPropagation();
$('div.styledSelect .active').each(function() {
$(this).removeClass('active').next('ul.options').hide();
});
$(this).toggleClass('active').next('ul.options').toggle();
});

// Hides the unordered list when a list item is clicked and updates the styled div to show the selected list item
// Updates the select element to have the value of the equivalent option
$listItems.click(function(e) {
e.stopPropagation();
$styledSelect.text($(this).text()).removeClass('active');
$this.val($(this).attr('rel'));
$listItems.show();
$(this).hide();
$list.hide();
/* alert($this.val()); Uncomment this for demonstration! */
});

// Hides the unordered list when clicking outside of it
$(document).click(function() {
$styledSelect.removeClass('active');
$list.hide();
});

});

我不确定我是否在那里得到你的问题,但你可能想在这里运行它 http://jsfiddle.net/v363gkj6/1/

我所做的是添加了一个

$listItems.show();
$(this).hide();

来自您的代码。下面是正在修改的代码:

$listItems.click(function(e) {
e.stopPropagation();
$styledSelect.text($(this).text()).removeClass('active');
$this.val($(this).attr('rel'));
$listItems.show();
$(this).hide();
$list.hide();
/* alert($this.val()); Uncomment this for demonstration! */
});

关于jquery - 如何在选择下拉列表中隐藏所选选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com