我有 1数组,其中我的源和目的地是这样的:
markers.push({
"Location": "Chicago",
"IsLocation": "Yes"
});
markers.push({
"Location": "Los Angeles",
"IsLocation": "Yes"
});
现在,当我用动态文本框创建点数时,我会这样做 喜欢在源和目的地之间添加所有点。
场景1 :第一个动态文本框,输入内容为例如:abc
markers[0]:Chicago
markers[1]:abc
marker[2]:Los Angeles.
场景2 :第二个动态文本框,输入说明例如:pqr
markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
marker[3]:Los Angeles.
场景3 :第三次动态文本框,输入内容为例如:lmn
markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
markers[3]:lmn
marker[4]:Los Angeles.
我的第一个职位将被修复。
代码:
// Code goes here
var cnt = 1;
var maxNumberOfTextboxAllowed = 5;
var autocomplete = [];
var markers = [];
markers.push({
"Location": "Chicago",
"IsLocation": "Yes"
});
markers.push({
"Location": "Los Angeles",
"IsLocation": "Yes"
});
function Generatetextbox() {
if (cnt <= maxNumberOfTextboxAllowed) {
var fieldWrapper = $("<div class='fieldwrapper' id='field" + cnt + "'/>");
var fName = $("<input type='text' class='fieldname' id='Txtopt" + cnt + "' name='TxtoptNm" + cnt + "' />");
fieldWrapper.append(fName);
fieldWrapper.append('<br />');
fieldWrapper.append('<br />');
$("#abc").append(fieldWrapper);
var newInput = [];
var newEl = document.getElementById('Txtopt' + cnt);
var txtboxId = 'Txtopt' + cnt;
newInput.push(newEl);
setupAutocomplete(autocomplete, newInput, 0, txtboxId);
cnt = cnt + 1;
} else
alert("Cant create more than 5 textbox")
}
function setupAutocomplete(autocomplete, inputs, i, txtboxId) {
autocomplete.push((txtboxId));
var idx = autocomplete.length - 1;
document.getElementById(autocomplete[idx]).addEventListener("change", function() {
alert(document.getElementById(autocomplete[idx]).value);
var autoTextbox = [{
"Location": document.getElementById(autocomplete[idx]).value,
"IsLocation": "Yes"
}]
var markerLastIndexData = [{
"Location": markers[markers.length - 1].Location,
"IsLocation": "Yes"
}]
markers[markers.length - 1] = autoTextbox;
markers[markers.length] = markerLastIndexData;
console.log(markers)
});
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="abc"></div>
<button onclick="Generatetextbox()" class="btn btn-primary" type="button">Add</button>
&#13;
您可以登录控制台。我的结果不正确。
预期输出显示在我的方案中,如:
marker[0]:{
Location="Chicago",
Isolcation="Yes"
}
marker[1]:{
Location="abc",
Isolcation="Yes"
}
etc......
答案 0 :(得分:2)
这种情况正在发生,因为在setupAutocomplete函数中,您将一个Array而不是Object分配给标记Array。只需删除声明将要推送到markers数组的两个变量的行上的[]。