简体   繁体   English

填充下拉列表

[英]Populating drop down list

I am have three drop down list : Country , State and City. 我有三个下拉列表:国家,州和城市。

I want to populate state list based on selection in country list and populate city list based on selection in state list. 我想根据国家列表中的选择来填充州列表,并根据州列表中的选择来填充城市列表。

Here's how I have attempted this 这是我尝试过的方式

  1. From the country list, using the onchage event I call java script function. 从国家列表中,使用onchage事件,我调用Java脚本函数。

     <select name="country" id="id1" onchange="onCountryChange(this.value)"> 
  2. Then I send a request to server to get names of states associated with country. 然后,我向服务器发送请求,以获取与国家/地区关联的州名。

     function onCountryChange(str){ if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("statenames").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","./view/countrychange.php?countryname="+str,true); xmlhttp.send(); } 
  3. The I get the values of states using the following code. 我使用以下代码获取状态值。

     <?php $countryname=$_GET["countryname"]; $connection = pg_connect("host=localhost port=5432 dbname=STAGE2 user=postgres password=jssate"); if (!$connection){ echo "<H1>Error in connection to Database</H1>"; echo "<H2>Please try again</H2>."; } else{ $query1="Select distinct(state) from master_table where country='$countryname'"; $result = pg_query($connection,$query1); $numrows = pg_numrows($result); if ($numrows>0){ echo "State <select name="state" id="stateid">; for ($row_index=0;$row_index<$numrows;$row_index++) { $state_name=pg_result($result,$row_index,0); echo "<option value="$state_name\\">$state_name</option>"; } echo "</select>"; } } pg_close($connection); ?> 

3 .Used the following code on my client side to display the result. 3。在客户端使用以下代码显示结果。

    <div id="statenames">
    //Original select list placed here is replaced by the one returned by server.
    </div>

This does work for me and the states are populated based on country name. 这确实对我有用,并且根据国家/地区名称填充了州。 Now how do I populate the third list. 现在,如何填充第三个列表。 Is the approach I have chosen for displaying the state list correct? 我选择的用于显示状态列表的方法是否正确? As I have no control on the select list of state which I get now. 由于我无法控制现在获得的状态选择列表。

Create 3 select country, city and state selects in your html page 在您的html页面中创建3个选择的国家,城市和州选择

  • only country select element will have option, state and city will be empty. 仅国家/地区选择元素具有选项,州和城市为空。
  • on change event of country send and ajax request to fetch state, and as the response send only option elements DO NOT WRAP THEM IN A SELECT as you have done 在国家/地区发送的更改事件和获取状态的Ajax请求发生更改时,作为响应仅发送选项元素, 请勿选择那样在选择中包装它们
  • Push these options into state's select element 将这些选项推入状态的选择元素

    document.getElementById('state').innerHTML(state_ajax_response); document.getElementById('state')。innerHTML(state_ajax_response);

Then create function in your html file that will be called on the state select change and again repeat the procedure for the cities select 然后在您的html文件中创建将在州选择更改时调用的函数,并再次针对城市选择重复该过程

Hope you get it. 希望你能明白。

Just send option elements as response. 只需发送选项元素作为响应即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM