我是新來的和Javascript所以請溫柔!使用if語句的Javascript變量值
我有一個的Html形式,我已經成功地照本宣科從谷歌地圖上我SQL數據庫和針點選定半徑與用戶的郵政編碼內查找商店。
有一個供用戶輸入郵編和兩個列表框的自由文本框,一個用於預設的半徑值,另一個用於預設的商店類型。如果存儲類型Type1
或Type2
選擇
我的代碼工作正常,但我希望用戶能夠選擇All
,並在地圖上同時返回Type1
和Type2
。
最明顯的事情對我來說是有一個,如果searchUrl變量(如下圖),說如果選擇All
然後使用URL不就完了+ '&BusinessType=' + businesstypechoice
部分,否則使用現有的URL中的語句。
不管多少我試圖寫if語句我無法讓表單/地圖工作,它不會返回任何值。
如何正確編寫if語句還是有更好的方法?
HTML
<input type="text" id="addressInput" placeholder="Enter Postcode" size="13"/>
<select id="radiusSelect">
<option value="10" >10 mi</option>
<option value="25" selected>25 mi</option>
<option value="50" >50 mi</option>
<option value="100">100 mi</option>
<option value="200">200 mi</option>
</select>
<select id="businesstypeSelect">
<option value="All" selected>All</option>
<option value="Type 1" >Type 1</option>
<option value="Type 2" >Type 2</option>
</select>
<input type="button" onClick="searchLocations()" value="Search"/>
的Javascript(部分從我相信這是與此有關功能)
var radius = document.getElementById('radiusSelect').value;
var businesstypechoice = document.getElementById('businesstypeSelect').value;
var searchUrl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' + radius + '&BusinessType=' + businesstypechoice;
downloadUrl(searchUrl, function(data) {
的功能繼續上。讓我知道你是否需要看更多。
在此先感謝!
編輯:
我在if語句的最新嘗試:
var searchUrl;
if(businesstypechoice == 'All') {
searchurl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' + radius;} else
if(businesstypechoice != 'All') {
searchurl = 'phpsqlsearch_genxml.php?Latitude=' + center.lat() + '&Longitude=' + center.lng() + '&radius=' + radius + '&BusinessType=' + businesstypechoice;}
downloadUrl(searchUrl, function(data) {
的phpsqlsearch_genxml.php文件的第一部分。
// Get parameters from URL
$center_lat = $_GET["Latitude"];
$center_lng = $_GET["Longitude"];
$radius = $_GET["radius"];
$business_type = $_GET["BusinessType"];
我想我需要的,如果爲$ business_type變量聲明,告訴它不要來查找BusinessType當「所有」被選擇because'All」並不在我的SQL數據庫在BusinessType列中存在。
您的解決方案看起來不錯。你能否提供描述如果陳述的代碼?也請告知您'phpsqlsearch_genxml.php'文件是由您編寫還是由第三方提供?問題可能存在於該文件中。 –
感謝您的評論。 這個php文件最初是由第三方編寫的,但我已經修改它以適應我的系統。 php文件的第一部分是: //從URL獲取參數 $ center_lat = $ _GET [「Latitude」]; $ center_lng = $ _GET [「Longitude」]; $ radius = $ _GET [「radius」]; $ business_type = $ _GET [「BusinessType」]; 所以我想你已經指出了我正確的方向。我現在想我的if語句工作正常,但我需要使用第二個php文件而沒有business_type部分它,因爲我沒有傳遞一個值,當我從列表框中選擇'All'時。 謝謝 – user3357285
你說得對。但不需要爲這種情況編寫單獨的文件。僅當$ business_type具有值時,才使用if語句將與$ business_type相關的部分添加到sql查詢中。 –