Jquery Select 2 未显示所选值
Jquery Select 2 not showing selected value
Select2 插件不显示带有 templateFormat 选项的选定选项
有人知道怎么解决吗?
标记:
<select id="plans" style="width: 75%">
<option value="1" name="text1" price="" saving="text" selected="selected"></option>
<option value="2" name="text2" price="" saving="text" selected="selected"></option>
js:
$(document).ready(function() {
$('select#plans').select2({
minimumResultsForSearch: Infinity,
templateResult: formatState
});
});
function formatState(state) {
if (!state.id) return state.text;
var html = '<span class="name">' + state.element.attributes.name.value + "</span>"
html += '<span class="price">' + state.element.attributes.price.value + </span>"
html += '<span class="saving">' + state.element.attributes.saving.value + "</span>"
var $state = $(html);
return $state
}
这里是上面代码的fiddle
谢谢
我明白了。进行了以下更改并开始工作。我添加了 templateSelection.
$(document).ready(function(){
$('select#plans').select2(
{minimumResultsForSearch: Infinity,
templateResult: formatState,
templateSelection:formatState
});
});
解决这个问题的一个非常简单的方法是:
// Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
// Step2: Now reinitialize your select box
// This will work, if you haven't initialized
selectbox $('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it
first and initialise it
$('#my_id').select2('destroy').select2();
Select2 插件不显示带有 templateFormat 选项的选定选项 有人知道怎么解决吗?
标记:
<select id="plans" style="width: 75%">
<option value="1" name="text1" price="" saving="text" selected="selected"></option>
<option value="2" name="text2" price="" saving="text" selected="selected"></option>
js:
$(document).ready(function() {
$('select#plans').select2({
minimumResultsForSearch: Infinity,
templateResult: formatState
});
});
function formatState(state) {
if (!state.id) return state.text;
var html = '<span class="name">' + state.element.attributes.name.value + "</span>"
html += '<span class="price">' + state.element.attributes.price.value + </span>"
html += '<span class="saving">' + state.element.attributes.saving.value + "</span>"
var $state = $(html);
return $state
}
这里是上面代码的fiddle
谢谢
我明白了。进行了以下更改并开始工作。我添加了 templateSelection.
$(document).ready(function(){
$('select#plans').select2(
{minimumResultsForSearch: Infinity,
templateResult: formatState,
templateSelection:formatState
});
});
解决这个问题的一个非常简单的方法是:
// Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
// Step2: Now reinitialize your select box
// This will work, if you haven't initialized
selectbox $('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it
first and initialise it
$('#my_id').select2('destroy').select2();