我对JavaScript很陌生,我似乎无法弄清楚为什么会这样。使用复选框我尝试在单击时将复选框的值添加到数组中。它好像加了两次。
这是HTML:
var placeT = [];
function initialize() {
// Default the map view to UK
var mapOptions = {
center: new google.maps.LatLng(55.3781, -3.4360),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 5
};
new AutocompleteDirectionsHandler(map);
$(".places").change(function() {
$(".places").each(function() {
if ($(this).is(':checked')) {
placeT.push($(this).val());
}
});
alert(placeT);
});
}
function findPlaces(boxes, searchIndex) {
var request = {
bounds: boxes[searchIndex],
types: [placeT]
};
// alert(request.bounds);
service.radarSearch(request, function(results, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert("Request[" + searchIndex + "] failed: " + status);
}
// alert(results.length);
document.getElementById('side_bar').innerHTML += "bounds[" + searchIndex + "] returns " + results.length + " results<br>"
for (var i = 0, result; result = results[i]; i++) {
var marker = createMarker(result);
}
searchIndex++;
if (searchIndex < boxes.length)
findPlaces(boxes, searchIndex);
});
}
以及相关的JS / JQuery:
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: lightgray;
}
ul {
position: absolute;
width: 100%;
height: 300px;
left: 0;
bottom: 0;
background-color: gray;
overflow-y: scroll;
}
li {
list-style: none;
}
#div {
position: absolute;
width: 100%;
height: 300px;
left: 0;
bottom: -300px;
background-color: darkgray;
}
button {
position: absolute;
right: 0;
bottom: 0;
}
我知道它可能很简单,但我似乎无法弄明白。谢谢你的帮助!
答案 0 :(得分:1)
更改您的代码如下:
$(".places").change(function()
{
placeT = [];
$(".places").each(function()
{
if( $(this).is(':checked') )
{
placeT.push($(this).val());
}
});
alert( placeT );
});