gpt4 book ai didi

javascript - 如何根据状态(选定、禁用)设置自定义选择类?

转载 作者:行者123 更新时间:2023-12-03 00:38:50 30 4
gpt4 key购买 nike

如何根据状态(选定、禁用)设置自定义选择类?如何根据状态选择(选定、禁用)在自定义下拉列表中设置类?照做了,但什么也没发生。请看我的例子............................................ ......................................

    $('select').each(function () {
var $this = $(this), numberOfOptions = $(this).children('option').length;

$this.addClass('select-hidden');
$this.wrap('<div class="select"></div>');
$this.after('<div class="select-styled"></div>');

var $styledSelect = $this.next('div.select-styled');
$styledSelect.text($this.children('option').eq(0).text());

var $list = $('<ul />', {
'class': 'select-options'
}).insertAfter($styledSelect);

for (var i = 0; i < numberOfOptions; i++) {
$('<li />', {
text: $this.children('option').eq(i).text(),
// class: component.children('option').eq(i).attr('class')
}).appendTo($list);

if ($(this).children('option[disabled]')) {
$('<li />').addClass('disabled-option');
} else if ($(this).children('option[selected]')) {
$('<li />').addClass('disabled-option');
}
}

var $listItems = $list.children('li');

$styledSelect.click(function (e) {
e.stopPropagation();
$('div.select-styled.active').not(this).each(function () {
$(this).removeClass('active').next('ul.select-options').hide();
});
$(this).toggleClass('active').next('ul.select-options').toggle();
});

$listItems.click(function (e) {
e.stopPropagation();
$styledSelect.text($(this).text()).removeClass('active');
// $this.val($(this).attr('class'));
$list.hide();
});

$(document).click(function () {
$styledSelect.removeClass('active');
$list.hide();
});
});
@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700");

body {
font-family: 'Open Sans', sans-serif;
font-size: 14px;
text-align: center;
}
h1 {
font-size: 60px;
font-weight: 400;
}
.select-hidden {
display: none;
visibility: hidden;
padding-right: 10px;
}
.select {
cursor: pointer;
display: inline-block;
position: relative;
width: 150px;
height: 40px;
margin-top: 50px;
text-align: left;
}
.select-styled {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border-radius: 25px;
background-color: transparent;
background-image: url('img/down-arrow.svg');
background-repeat: no-repeat;
background-size: 15px;
background-position: 90%;
border: 1px solid #ccc;
padding: 10px 25px;
&:active, &.active {
// z-index: 9999;
}
}

.select-options {
display: none;
position: absolute;
top: 40px;
left: 0;
width: 100%;
max-height: 220px;
margin: 0;
padding: 10px 0;
background-color: #fff;
box-shadow: 0 7px 15px 0 rgba(225, 225, 225, 0.7);
list-style: none;
z-index: 999;
overflow-y: auto;
li {
margin: 0;
padding: 10px 0;
text-indent: 15px;

&:hover {
background-color: #ddd;
}
&[rel="hide"] {
display: none;
}
}
}

.hidden {
pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select name="select" id="">
<option value="1" disabled>1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
</select>

最佳答案

如果您需要在初始渲染时向选择元素中的某些特定选项添加类,可以使用以下解决方案。

$(document).ready(function(){
$('.select-options option').each(function(i, e){
//console.log(e.disabled);
if(e.disabled) $(e).addClass('disabled');
else if(e.selected) $(e).addClass('active');
});
});
.select-options {}
.select-options option {}
.select-options option.disabled {color: blue;}
.select-options option.active {color: red;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="" class="select-options">
<option value="dis" disabled>Disabled</option>
<option value="acv" selected>Selected</option>
<option value="anv">Another One</option>
<option value="hello">Hello</option>
</select>

<小时/>

请记住,您还可以设置/更改禁用/选定元素的样式,如下所示:

CSS:

select option:disabled {
color: #000;
font-weight: bold;
}
<小时/>

如果您需要在更改时添加类,请使用以下代码:

$(document).ready(function(){
$(".select-options").get(0).addEventListener("change", function(event) {
var select = $(".select-options").get(0);
var options = select.options;
var selected = select.options[select.selectedIndex];
$(selected).addClass("active").siblings().removeClass("active");
}, false);
});

附注如果您需要任何进一步的解释,请在下面的评论部分中询问。

<小时/>

编辑:我给了你这个想法,但如果需要修改你的示例,请使用此代码。 (将其替换为 for 循环):

for(var i = 0; i < numberOfOptions; i++){
if($this.children('option').eq(i).get(0).disabled){
$('<li class="disabled-option">'+ $this.children('option').eq(i).text() +'</li>').appendTo($list);
} else if($this.children('option').eq(i).get(0).selected){
$('<li class="active-option">'+ $this.children('option').eq(i).text() +'</li>').appendTo($list);
} else {
$('<li class="">'+ $this.children('option').eq(i).text() +'</li>').appendTo($list);
}
}

关于javascript - 如何根据状态(选定、禁用)设置自定义选择类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53547350/

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