我有这样的 dropdown
。
<input id="tripFrom" type="text"
angular-select2="{type:'typeahead',placeholder:'- Select -'}"
select-typeahead-params="{removeTripId:currentTrip.customerDetails.tripToID, type:'startsFrom'}"
select-typeahead-url="getTripFrom('startsFrom')"
ng-model="currentTrip.fromTripID"
select-text="currentTrip.startingAreaName"
/>
对于这个下拉菜单
,我得到的数据是这样的:
{"success":true,
"items":[
{"id":56565,"text":"andra"},
{"id":56568,"text":"tamilnadu"}
]
}
我的问题是当我在 dropdown
中添加一些新文本时(dropdown
中不存在)我需要将其创建为新的 object
在 items
中。
当我blur
dropdown
时,新添加的数据应该显示在所选的dropdown
中。
这是我添加的:
$('#s2id_autogen8_search').change(function(){
var data=[];var items={};
var newAddress=$('#s2id_autogen8_search').val();
var newId=Math.floor(Math.random()*1000000+1);
var newTripAddress={id:newId,text:newAddress}
items.id=newId;
items.text=newAddress;
data.push(items)
/*$("#tripFrom").select2({
data : newTripAddress})*/
});
但是当我模糊
时我的输入没有显示。我做错了什么?
最佳答案
尝试使用模糊而不是更改事件。
$('#s2id_autogen8_search').change(function(){
// use this instead
$('#s2id_autogen8_search').on('blur', function(){
可能就这么简单。
关于javascript - 将新对象添加到现有的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45959761/