0

I need some ajax code to do some functions with drop down. I have three table of values: one for country, then state and city. And I have three dropdown to show these values.

First it should show:

  • select country
  • select state
  • select city

in that drop down(select) respectively.

When I select united kingdom from the first dropdown, in the second drop down it must show the states of the united kingdom. And again when I select a state from the state dropdown it must show the cities of that state in the third dropdown(city).

I want to do this with ajax. Does any one have a code to do this?

4

3 回答 3

0

片段http://coffeestain-it.blogspot.fr/2011/08/mvc3-cascading-lists-using-ajax-and.html?m=1

于 2012-07-27T05:35:41.947 回答
0

我已经做了你需要的 去这里

选择类型、位置。如果您需要相同的内容,我会这样做,我将发布我的代码,您可以修改它

更新 我要从上面的网站复制代码,我让你修改它。我将粘贴“类型”下拉列表的代码,你可以这样做,而不是为其他人做。一件事是它非常古老的项目,所以我是通过 Javascript 而不是 jQuery 来完成的,希望它会激怒你。:(

<td>Type</td>
<td>
      <select  id="type" onChange="propertyType(this.value)" name="type">
                            <option value="">All</option>
                            <option value="homes">Homes</option>
                            <option value="plots">Plots</option>
                            <option value="commercial">Commercial</option>
      </select>
</td>

这是propertyType的js

function propertyType(str){
  if(str=='' || str=='plots'){
    document.getElementById("type_h").innerHTML=""; 
    document.getElementById("bed").innerHTML="";    
    }   
    else if(str=='commercial'){
    document.getElementById("bed").innerHTML="";    
          }
 else{
  document.getElementById("type_h").innerHTML="<img src='<?php echo $serverimageurl?>ajax-loader-small.gif' />";
    if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
 }
 else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
    xmlhttp.onreadystatechange=function()
   {
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("type_h").innerHTML=xmlhttp.responseText;
    }
   }
   xmlhttp.open("GET","ajax/propertytype.php?s="+str,true);
   xmlhttp.send();
   }
 }

这是propertytype.php

<?php
  $s=$_GET["s"];
  if($s=="homes"){
   ?>


 <select onchange="ajax_bed(this.value)" name="subtype" id="subtype" >
  <option value="">Type Of Houses</option>
  <option value="houses">Houses</option>
  <option value="flats">Flats</option>
  <option value="farmhouses">Farm Houses</option>
</select>

<?php
}
 if($s=="plots")
{
?>


 <?php
  }
  if($s=="commercial")
  {
  ?>
  <select name="subtype" id="subtype" >
   <option value="offices">Offices</option>
   <option value="shops">Shops</option>
   <option value="warehouses">Warehouses</option>
   <option value="factories">Factories</option>
   <option value="building">Buildings</option>
   <option value="other">Other</option>
  </select>
   <?php
  }
?>

这是用于选择卧室数量的ajax函数

function ajax_bed(str){
  document.getElementById("bed").innerHTML="<img src='<?php echo $serverimageurl?>ajax-loader-small.gif' />";
  if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
   }
  else
    {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
    xmlhttp.onreadystatechange=function()
   {
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("bed").innerHTML=xmlhttp.responseText;
    }
   }
   xmlhttp.open("GET","ajax/bedroomsselection.php?t="+str,true);
   xmlhttp.send();
 }

这是卧室选择.php

<?php
$t=$_GET["t"];
if($t=="houses"||$t=="flats"||$t=="farmhouses")
{
?>
 <select id="bed" name="bed">
   <option>None</option>
   <option>Single Bed</option>
   <option>Double Bed</option>
   <option>three Bed</option>
   <option>Four Bed</option>
   <option>Five Bed</option>
   <option>Six Bed</option>
   <option>Seven Bed</option>
   <option>Eight Bed</option>
   <option>Ten Bed</option>
   <option>More Than Ten Bed</option>
</select>


 <?php
 }
?>

我希望你现在已经有了这个想法,是时候自己编写代码了 干杯

于 2012-07-27T05:37:06.540 回答
0

如果您已经熟悉 jQuery,您可能想查看如何通过 $.ajax() ( http://api.jquery.com/jQuery.ajax/ ) 发送 ajax 请求 您可以添加更新列表的代码成功选项中的州和城市,并通过将 onchange 事件绑定到州和国家下拉列表来触发发送 ajax 调用。您可以使用 .bind() ( http://api.jquery.com/bind/ ) 来执行此操作。

于 2012-07-27T05:24:47.750 回答