我是新来的和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查询中。 –