【发布时间】:2014-05-01 15:22:52
【问题描述】:
我正在为一些 UI 元素使用 Bootstrap:SelectPicker,它允许用户选择多个选项并在段落标签中将其呈现到屏幕上。他们还应该能够删除选定的选项。
这是我将所选选项呈现到屏幕上的代码,这样每个选项旁边都会显示一个“X”,当单击它时,所选项目会从屏幕中删除:
//Render selected item to the screen
$('#dataCombo').change(function(){
$('#dataOutput').html('');
var values = $('#dataCombo').val();
for(var i = 0; i < values.length; i += 1) {
$('#dataOutput').append("<p class='removeable'>" + values[i] + " x </p>")
}});
//When the 'X' is clicked, remove that item
$("#dataOutput").on('click','.removeable',function(){
$(this).remove(); //this removes the item from the screen
//Next i need to unselect it from dataCombo selectpicker
var foo = $(this);
$('#dataCombo').find('[value=foo]').remove();
console.log(foo);
$('dataCombo').selectpicker('refresh');
});
So the problem is the second half of the 'remove' code, while the item does get removed from the output display, it is still selected in the select picker, so when another item is selected - the 'removed' item is重新渲染。有什么想法我可以做到这一点吗?
HTML 非常简单:
<h6>ComboBox</h6>
<select id="dataCombo" class="selectpicker" multiple>
</select>
【问题讨论】:
标签: javascript jquery html twitter-bootstrap