twitter-bootstrap 如何从 Bootstrap Multi-Select 返回文本值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25974956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return text values from Bootstrap Multi-Select
提问by Hagbard
I'm using bootstrap select for a multiple enabled dropdown. When a button is pressed i'm building a variable to use in an AJAX request.
我正在使用引导程序选择来启用多个下拉列表。当按下按钮时,我正在构建一个变量以在 AJAX 请求中使用。
It works for returning the values (basically the ID's in this case), since it returns an array of values, i can use a loop to build the JSON. However i also need to send the text value, but havent been able to get this working.
它用于返回值(在这种情况下基本上是 ID),因为它返回一个值数组,我可以使用循环来构建 JSON。但是,我还需要发送文本值,但无法使其正常工作。
$('#sendButton').click(function() {
var modal = $('#modalPopup');
var modalID = modal.data('value');
var selectedOptionValue = $('#multiSelectBox').val();
var selectedOptionName = $('#multiSelectBox').text();
var sendRequest = {
'modalNo': modalID,
'products': []
};
for (var i = 0; i < selectedOptionValue.length; i++) {
sendRequest.products.push({
'productId': selectedOptionValue[i],
'productName': selectedOptionName
});
}
});
I tried another loop to do selectedOptionName[j]when building the request but no luck, i end up getting all of the text values in my drop down. Tried using:
我selectedOptionName[j]在构建请求时尝试了另一个循环,但没有运气,我最终在下拉列表中获得了所有文本值。尝试使用:
var selectedOptionName = $('#multiSelectBox:selected').text();
But this doesn't return anything. Any ideas?
但这不会返回任何东西。有任何想法吗?
HTML for drop down:
用于下拉列表的 HTML:
<select class="selectpicker" multiple id="multiSelectBox" title="Select Product(s)"></select>
Code to populate dropdown:
用于填充下拉列表的代码:
var html = '';
for (var i = 0; i < data.products.length; i++){
html += '<option value ="' + data.products[i].productId+ '">' + data.products[i].productName+ '</option>'
}
$('#multiSelectBox').html(html);
$('.selectpicker').selectpicker('refresh');
回答by SSA
The correct syntax is
正确的语法是
$("#multiSelectBox option:selected").text()
$("#multiSelectBox option:selected").text()
but this will give you a string of all selected text. So you need to find the text by value in your loop.I didn't check for syntax here, there might be typos.
但这会给你一个所有选定文本的字符串。所以你需要在循环中按值查找文本。我没有在这里检查语法,可能有错别字。
for (var i = 0; i < selectedOptionValue.length; i++) {
var val = selectedOptionValue[i];
var txt = $("#multiSelectBox option[value='"+val+"']").text();
sendRequest.products.push({
'productId': val ,
'productName': txt
});
}
回答by Shiva
SELECT ALL WITH FILTER
用过滤器全选
<h1 class="monthofyear">monthofyear: <span></span></h1>
$(document).ready(function() { $('#monthofyear').multiselect({ includeSelectAllOption: true,
onSelectAll: function(element, checked) {
var selected = [];
$('#monthofyear option:selected').each(function(index, brand) {
selected.push([$(this).val()]);
});
$("h1.monthofyear span").text(selected);
},
enableCaseInsensitiveFiltering: true,
enableFiltering: true,
maxHeight: '300',
buttonWidth: '235',
onChange: function(element, checked) {
var brands = $('#monthofyear option:selected');
var selected = [];
$(brands).each(function(index, brand) {
selected.push([$(this).val()]);
});
$("h1.monthofyear span").text(selected);
}
});});

