2017-04-03 28 views
0

使用複選框值來填充數組,然後加入那些我需要把括號中加入元素周圍開展了谷歌的地方類型的請求元素。似乎無法把周圍連接結果括號中的JavaScript

這些是複選框:

<form id= "checkboxes"> 
<label> Types of Places</label> 
<link rel="stylesheet" type="text/css" href="css/style.css"> 
    <input type="checkbox" name="cbox" value="woodland" class="places"/> 
    <label for="woodland"><img src="../img/Woodland.jpg" /></label> 
    <input type="checkbox" name="cbox" value="lodging" class="places"/> 
    <label for="viewpoint"><img src="../img/Viewpoint.jpg" /></label> 
    <input type="checkbox" name="cbox" value="restaurant" class="places"/> 
    <label for="restaurant"><img src="../img/Restaurant.jpg" /></label> 
    <input type="checkbox" name="cbox" value="museum" class="places" /> 
    <label for="historical"><img src="../img/Historical.jpg" /></label> 
    </form> 

</form> 

這是用於獲取複選框的代碼值到陣列:

$(".places").change(function() 
    { 
     placeT = []; 
     $(".places").each(function() 
     { 
      if($(this).is(':checked')) 
      { 
       placeT.push($(this).val()); 
      } 
     }); 
     alert(placeT); 
    }); 

這是JavaScript用於接合元件和執行地點搜索:

function findPlaces(boxes,searchIndex) { 

    var typeP = placeT.join('", "'); 

    var request = { 
     bounds: boxes[searchIndex], 
     types: typeP 
    }; 

    //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); 
    }); 
} 

types的最終格式例如,該請求需要是[「博物館」,「餐館」]。搜索工作正常types: ["museum", "restaurant"],但我似乎無法圍繞typeP結果括號。任何幫助深表感謝!

+0

你想對你的地方數組作爲JSON字符串?如果是使用JSON.stringify()它會給你你的數組的JSON。 –

+0

你缺少外引號:'VAR typeP = ' 「+ placeT.join('」, 「 ')+'」';'而確實需要圍繞它brakets? – Kjell

+0

只需傳遞如下數組:var request = { bounds:boxes [searchIndex], types:placeT };而不使用join()。 – Alok

回答

0

只需傳遞如下數組:var request = {bounds:boxes [searchIndex],types:placeT};不使用join()方法