gpt4 book ai didi

javascript - 让每个功能结果显示分开

转载 作者:行者123 更新时间:2023-11-29 21:06:41 25 4
gpt4 key购买 nike

我有一个 div,用作选择的着陆点,有点像自定义选择输入。如果单击“进行选择”,下面会出现一个菜单。然后您的选择(一项或多项)将显示在该 div 中。

我有两个无法解决的问题。

  1. 如何让每个选择显示在 .drop-item-selected 中,以便选择看起来像单独的框。 (现在,如果选择了多个选项,则 div 会包裹所有选项)。

  2. 如果 .drop-item-inputs 从下拉列表中选中,如何删除它们。注释掉的 javascript 是我尝试过的,但这只是删除了整个列表。

有人有什么想法吗?

对于 #1,我希望盒子分别包裹在 div 中,因此它们看起来像这样:

enter image description here

Here is a jsfiddle.

$('#proposal-type').click(function() {
$('#proposal-type-drop').addClass('active');
});
$('.drop-item-input').on('change', function() {
var proposalVal = "";
$('.drop-item-input').each(function() {
if ($(this).is(":checked")) {
proposalVal += $(this).val();
//$(this).fadeOut();
};
/*if ($('.drop-item-input').is(':checked')) {
$('.drop-item').fadeOut();
}*/
$('#proposal-type').val(proposalVal).html("<div class='drop-item-selected'>" + proposalVal + "</div>");
$('#proposal-type-drop').removeClass('active');
});
});
#proposal-type-drop {
width: 45%;
display: none;
position: absolute;
}

#proposal-type-drop.active {
background: rgba(0, 0, 0, 1);
display: block;
height: auto;
z-index: 1;
}

[contentEditable=true]:empty:not(:focus):before {
content: attr(data-text)
}

.drop-item {
color: #FFF;
font-size: .9rem;
padding: 5px;
background: #000;
display: block;
width: 100%;
cursor: pointer;
}

.drop-item-input {
display: none;
}

.drop-item-selected {
background: blue;
padding: 5px;
font-size: .9rem;
width: auto;
display: inline-block;
margin: 0 3px;
}

.proposal-text {
width: 95%;
display: block;
height: 6em;
margin: 1.5% 2% 2.5% 2%;
!important
}

#proposal-check {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="proposal-type" contentEditable=true name="proposal_type" class="proposal-input" data-text="Make Selection"></div>
<div id="proposal-type-drop">
<label class="drop-item">A<input type="checkbox" class="drop-item-input" value="A"></label>
<label class="drop-item">B<input type="checkbox" class="drop-item-input" value="B"></label>
<label class="drop-item">C<input type="checkbox" class="drop-item-input" value="C"></label>
</div>

最佳答案

对于您要求的,只需执行:

$('#proposal-type').click(function() {
$('#proposal-type-drop').addClass('active');
});
$('.drop-item-input').on('change', function() {
var proposalVal = "";
var proposalHtml = "";
$('.drop-item-input').each(function() {
if ($(this).is(":checked")) {
proposalVal += $(this).val();
proposalHtml += '<span class="drop-item-selected">' + $(this).val() + '</span>';
$(this).closest('label').fadeOut();
};
$('#proposal-type').val(proposalVal).html(proposalHtml);
$('#proposal-type-drop').removeClass('active');
});
});
#proposal-type-drop {
width: 45%;
display: none;
position: absolute;
}

#proposal-type-drop.active {
background: rgba(0, 0, 0, 1);
display: block;
height: auto;
z-index: 1;
}

[contentEditable=true]:empty:not(:focus):before {
content: attr(data-text)
}

.drop-item {
color: #FFF;
font-size: .9rem;
padding: 5px;
background: #000;
display: block;
width: 100%;
cursor: pointer;
}

.drop-item-input {
display: none;
}

.drop-item-selected {
display: inline-block;
background: blue;
padding: 5px;
font-size: .9rem;
width: auto;
display: inline-block;
margin: 3px;
}

.proposal-text {
width: 95%;
display: block;
height: 6em;
margin: 1.5% 2% 2.5% 2%;
!important
}

#proposal-check {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="proposal-type" contentEditable=true name="proposal_type" class="proposal-input" data-text="Make Selection"></div>
<div id="proposal-type-drop">
<label class="drop-item">A
<input type="checkbox" class="drop-item-input" value="A">
</label>
<label class="drop-item">B
<input type="checkbox" class="drop-item-input" value="B">
</label>
<label class="drop-item">C
<input type="checkbox" class="drop-item-input" value="C">
</label>
</div>

也在 JSFiddle 上.

关于javascript - 让每个功能结果显示分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43727545/

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