【发布时间】:2017-05-01 22:43:29
【问题描述】:
我有一个div,它用作选择的着陆场所,类似于自定义选择输入。如果单击“进行选择”,则会在下方显示一个菜单。然后您的选择(一个或多个项目)将出现在该 div 中。
我有两个我无法弄清楚的问题。
如何让每个选择显示在
.drop-item-selected中,以便选择看起来像单独的框。 (现在 div 包含所有选择选项,如果选择了多个选项)。如果从下拉列表中选中
.drop-item-inputs,如何删除它们。注释掉的 javascript 是我尝试过的,但这只是删除了整个列表。
有人有什么想法吗?
对于 #1,我希望将盒子分别包装在 div 中,因此它们看起来像这样:
$('#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>
【问题讨论】:
-
如果你想在 contentEditable 中创建标签,那么你需要更多的逻辑。例如,如果有人键入“A”,那么您需要将其包装在 span 中并在下拉列表中检查/隐藏它,对吗?另外,您是否允许输入“F”,因为它不在列表中?
-
@skobaljic 标签与 contentEditable 无关。我正在制作的“标签”是单独的输入,点击时会出现。
-
我仍然不清楚目标是什么,您需要什么功能。您在这个问题中提出的问题很简单,但我怀疑答案会对您有所帮助,因为我认为还有更多工作要做。
-
@skobaljic 我添加到我的问题中以澄清。
-
好的,对于 1 只需执行 this way,创建要附加的 html。
标签: javascript jquery function each