[英]Add new object into existing json
I have dropdown
like this. 我有这样的
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"
/>
For this dropdown
I am getting data like this: 对于此
dropdown
我将获得如下数据:
{"success":true,
"items":[
{"id":56565,"text":"andra"},
{"id":56568,"text":"tamilnadu"}
]
}
My question is when I add some new text in dropdown
(which is not present in dropdown
)I need to create its as new object
inside of items
. 我的问题是,当我加入一些新的文本
dropdown
(这是不是在目前的dropdown
),我需要建立其作为新object
的内部items
。
When I blur
the dropdown
new added data should be displayed in that dropdown
as selected. 当我
blur
dropdown
应该在该dropdown
显示新添加的数据。
This what I have added: 这是我添加的内容:
$('#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})*/
});
But my input is not displaying when I blur
it. 但是,当我
blur
输入时,我的输入不会显示。 What I did wrong? 我做错了什么?
Try using the blur instead of the change event. 尝试使用模糊而不是更改事件。
$('#s2id_autogen8_search').change(function(){
// use this instead
$('#s2id_autogen8_search').on('blur', function(){
It might be as simple as that. 可能就这么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.