如何使用select2在输入框上设置默认值?这是我的HTML
<input type="text" id="itemId0" value="Item no. 1">
Run Code Online (Sandbox Code Playgroud)
和我的javascript:
$("#itemId0").select2({
placeholder: 'Select a product',
formatResult: productFormatResult,
formatSelection: productFormatSelection,
dropdownClass: 'bigdrop',
escapeMarkup: function(m) { return m; },
minimumInputLength:1,
ajax: {
url: '/api/productSearch',
dataType: 'json',
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {results:data};
}
}
});
function productFormatResult(product) {
var html = "<table><tr>";
html += "<td>";
html += product.itemName ;
html += "</td></tr></table>";
return html;
}
function productFormatSelection(product) {
var selected = "<input type='hidden' name='itemId' …Run Code Online (Sandbox Code Playgroud) 我在我的webapplication中使用Select2.我用Ajax加载我的Select2框.验证失败时,除Select2框外,所有输入都按照之前的方式填充.如何在表单验证失败后恢复旧值?我的赌注是使用Request::old('x'),但这会插入值(在我的情况下是用户ID)而不是所选文本.因此,例如文本John将变为27选择框.我怎样才能收回文字?
<select id="customer" name="customer" class="searchselect searchselectstyle">
</select>
Run Code Online (Sandbox Code Playgroud)
js:
token = '{{csrf_token()}}';
$(".searchselect").select2({
ajax: {
dataType: "json",
type: "POST",
data: function (params) {
return {
term: params.term,
'_token': token,
'data' : function(){
var result = [];
var i = 1;
$('.searchselect').each(function(){
result[i] = $(this).val();
i++;
});
return result;
}
};
},
url: function() {
var type = $(this).attr('id');
return '/get' + type;
},
cache: false,
processResults: function (data) {
return {
results: data
};
} …Run Code Online (Sandbox Code Playgroud) 我想在默认情况下将第一个位置设置为select2下拉列表,我已经尝试了这个但它不起作用:
$('#mylist').val($('#mylist option:first-child').val()).trigger('change');
Run Code Online (Sandbox Code Playgroud)
我试过的其他方式;
$('#mylist').val(1);
Run Code Online (Sandbox Code Playgroud)
但问题是我不知道它是什么价值,因为它取决于查询,它并不总是相同的值.
我没有设置HTML中的下拉值,但它是隐藏的输入,并且值在查询中加载
我希望有人能帮助我
问候!