【发布时间】:2015-10-21 11:11:32
【问题描述】:
我知道这个问题已经被问过好几次了,但实际上我已经研究了 4 个小时的解决方案,但找不到针对我的具体问题的解决方案。
我在 Laravel 刀片编辑视图上有一个 select2 选择字段,我想将它的值设置为传递给视图的数据。将数据传递给视图已经发生了,使用选择字段也没有问题,它工作得很好。但是我一直在尝试的每一次尝试都无法设置它。我一直在搞乱一个按钮来设置选择字段的值,但是每次修改
$("select[name=customer]").val("1");
未能做到这一点。它总是传输“?id = null”。我会很感激有人对此有所了解,因为没有这个我真的无法继续。我在下面添加重要的代码部分,如果您需要更多内容,请回复。提前致谢!
选择 HTML:
<label>*Customer</label>
<select class="form-control" name="customer" id="customer" style="width: 100%">
<option></option>
</select>
选择2:
$("select[name=customer]").select2({
allowClear : true,
placeholder : "Choose customer",
ajax : {
url : "/search/autocomplete/get_customers",
dataType : 'json',
type : "GET",
quietMillis : 50,
delay : 250,
data : function(params) {
return {
filter : params.term
};
},
processResults : function(data) {
return {
results : $.map(data, function(item) {
return {
text : item.name + ' (' + item.customer_code + ')',
id : item.id
}
})
};
},
initSelection : function (element, callback) {
var data = [];
$(element.val()).each(function () {
data.push({id: this, text: this});
});
callback(data);
}
}
});
按钮:
<button type="button" onclick="test()">Test</button>
函数测试():
function test() {
$("select[name=customer]").val("1");
};
我一直在尝试对此进行一些更改,示例:
$("select[name=customer]").val("1");
$("select[name=customer]").val("1").change();
$("select[name=customer]").val("1").trigger("change");
$("select[name=customer]").select2("val", "1");
列表继续。
如果我没有提供重要的东西,请给我留言或重播!
最好的问候
尼克
【问题讨论】:
标签: javascript laravel default jquery-select2