我有一个div字段,单击它时会创建一个下拉菜单。每当您点击下拉选项选项时,名称将转到div字段#proposal-type
。您可以继续单击以添加其他选项。
我的问题是,当我点击x删除选择,我不知道如何获取只需点击删除并填充到下拉列表中的整个drop-item
。现在只有x回到选项列表中。
有没有人有任何想法?
另外,有人可以请分享一个快速指针,如何让选项框列表消失,当我点击它之外?无论现在如何,它都会保持在那里。
$('#proposal-type').click(function() {
$('#proposal-type-drop').addClass('active');
});
$('#proposal-type').text("Type of Project *");
$('.drop-item-input').on('change', function() {
var proposalVal = "";
var proposalHtml = "";
$('.drop-item-input').each(function() {
if ($(this).is(":checked")) {
proposalVal += $(this).val();
proposalHtml += '<div class="drop-item-selected"><span class="drop-item-close"></span>' + $(this).val() + '</div>';
$(this).closest('label').fadeOut();
};
//else if ($(this).is(":checked") === false) {
// \t $(this).closest('label').fadeIn();
//};
$('#proposal-type').val(proposalVal).html(proposalHtml);
$('#proposal-type-drop').removeClass('active');
});
//values
var type = $('.drop-item-input:checked').map(function() {
return $(this).val();
}).get().join(', ');
console.log(type);
});
$(document).on('click', '.drop-item-close', function() {
console.log("Triggered");
$(this).fadeOut("medium", function() {
$(this).detach().appendTo('#proposal-type-drop').fadeIn();
});
$(this).is(":checked") === false;
$(this).closest('label').fadeIn();
$(this).closest('.drop-item-selected').fadeOut();
});
.panel-input {
box-sizing: border-box;
border: 1px solid #858585;
display: inline-block;
vertical-align: top;
width: 45%;
margin: 1.5% 2% 2.5% 2%;
}
.proposal-input {
width: 100%;
}
#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;
}
.drop-item {
color: #FFF;
font-size: 1rem;
padding: 7px 5px;
font-family: 'Open Sans', sans-serif;
background: rgba(0, 0, 0, .8);
display: block;
width: 100%;
cursor: pointer;
}
.drop-item-input {
display: none;
}
.drop-item-selected {
background: #0085A1;
color: #333333;
padding: 5px;
font-size: .9rem;
font-weight: bold;
font-family: 'Roboto', sans-serif;
width: auto;
display: inline-block;
margin: 0 3px;
}
.drop-item-close {
display: inline-block;
background-image: url("http://www.clipartbest.com/cliparts/dT8/xnA/dT8xnAqAc.png");
background-size: 10px 10px;
background-repeat: no-repeat;
height: 10px;
width: 10px;
margin-right: 10px;
cursor: pointer;
transition: ease 0.1s;
-webkit-transition: ease 0.1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="panel-input">
<div id="proposal-type" class="proposal-input"></div>
<div id="proposal-type-drop">
<label class="drop-item">Apple<input type="checkbox" name="prop-type[]" class="drop-item-input" value="Apple"></label>
<label class="drop-item">Banana<input type="checkbox" name="prop-type[]" class="drop-item-input" value="Banana"></label>
<label class="drop-item">Cake<input type="checkbox" name="prop-type[]" class="drop-item-input" value="Cake"></label>
</div>
</div>
在点击处理程序中,this指的是被单击的元素,它是X.您应该可以使用'this'通过$(this).parent()来定位正确的值。 ()'或类似 – James
@James谢谢你的帮助。我有点想通了,你的建议,但它开始搞砸了,当你尝试添加一个项目回到选定的状态.. https://jsfiddle.net/xrrpw1mq/1/ – Paul
另外,我似乎无法弄清楚如何删除与之相关的选中值。 – Paul