selectpicker 清空选项_javascript – 在Bootstrap selectpicker上使用jQuery取消选择一个选项...

我正在使用Bootstrap来处理一些UI元素: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("

" + values[i] + " x

")

}});

//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');

});

所以问题是’删除’代码的后半部分,而项目确实从输出显示中删除,它仍然在选择选择器中被选中,因此当选择另一个项目时 – 重新渲染’已删除’项.我有什么想法可以做到这一点?

HTML非常简单:

ComboBox

解决方法:

这是一个使用deselectAll()方法的工作示例.

JS:

//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("

" + values[i] + " x

")

}});

//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.find('.reel').html()+']').remove();

// $('#dataCombo').val( );

$values = $('#dataCombo').val();

$('#dataCombo').selectpicker('deselectAll');

$('#dataCombo').selectpicker('val', $values );

$('#dataCombo').selectpicker('refresh');

});

$("#dataCombo").selectpicker({

multiple:true

});

更新:

更改

$('#dataCombo').find('[value='+foo.find('.reel').html()+']').remove();

通过

$('#dataCombo').find('[value='+foo.find('.reel').html()+']').prop('selected', false);

标签:jquery,javascript,twitter-bootstrap,html

来源: https://codeday.me/bug/20190717/1491244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值