欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ajax實(shí)現(xiàn)簡(jiǎn)單下拉選項(xiàng)效果【推薦】

 更新時(shí)間:2016年04月19日 17:21:43   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇Ajax實(shí)現(xiàn)簡(jiǎn)單下拉選項(xiàng)效果【推薦】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過(guò)來(lái)看看吧

基本都是固定步驟!主要在JAVASCRIPT和PHP中的操作

1、HTML代碼里就只有兩個(gè)SELECT標(biāo)簽如下:

<select id="province">
  <option>請(qǐng)選擇</option>
 </select>
 <select id="city">
  <option>請(qǐng)選擇</option>
 </select>

2、Javascript中進(jìn)行創(chuàng)建選項(xiàng)和執(zhí)行AJAX異步請(qǐng)求步驟如下

<script> 
  var xhr = getXhr(); 
  // 第一次執(zhí)行Ajax異步請(qǐng)求 - 省份 
  window.onload = function(){ 
    xhr.open("get","finaly.php?state=1"); 
    xhr.send(null); 
    xhr.onreadystatechange = function(){ 
    if(xhr.readyState==4&&xhr.status==200){ 
        var data = xhr.responseText; 
        // 將字符串轉(zhuǎn)換為數(shù)組 
        var provinces = data.split(","); 
        // 遍歷數(shù)組 
        for(var i=0;i<provinces.length;i++){ 
          // 創(chuàng)建option元素添加到id為province元素上 
          var option = document.createElement("option"); 
          var text = document.createTextNode(provinces[i]); 
          option.appendChild(text); 
          var province = document.getElementById("province"); 
          province.appendChild(option); 
        } 
      }  
    } 
  } 
  // 第二次執(zhí)行Ajax異步請(qǐng)求 - 城市 
  var provinceEle=document.getElementById("province"); 
  provinceEle.onchange = function(){ 
    var city = document.getElementById("city"); 
    var opts = city.getElementsByTagName("option"); 
    for(var z=opts.length-1;z>0;z--){ 
      city.removeChild(opts[z]); 
    } 
     
    if(province.value != "請(qǐng)選擇"){ 
      xhr.open("post","finaly.php"); 
      xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
      xhr.send("province="+province.value); 
      xhr.onreadystatechange = function(){ 
        if(xhr.readyState==4&&xhr.status==200){ 
          var data = xhr.responseText; 
          var cities = data.split(","); 
          for(var i=0;i<cities.length;i++){ 
            var option = document.createElement("option"); 
            var text = document.createTextNode(cities[i]); 
            option.appendChild(text); 
             
            city.appendChild(option); 
          } 
        } 
      } 
    } 
 
  } 
 
  function getXhr(){ 
    var xhr = null; 
    if(window.XMLHttpRequest){ 
      xhr = new XMLHttpRequest(); 
    }else{ 
      xhr = new ActiveXObject("Microsoft.XMLHttp"); 
    } 
    return xhr; 
  } 
 </script>

3、PHP代碼如下:文件名為:finaly.php與JS中:xhr.open(method,url)方法的url對(duì)接!

<?php 
  // 接收客戶端發(fā)送的請(qǐng)求數(shù)據(jù) - state 
  $state = $_REQUEST['state']; 
  // 判斷$state的值 
  if($state == 1){// 獲取省份 
    echo '山東省,遼寧省,吉林省'; 
  }else{// 獲取城市 
    $province = $_POST['province']; 
    switch ($province){ 
      case '山東省': 
        echo '青島市,濟(jì)南市,威海市,日照市,德州市'; 
        break; 
      case '遼寧省': 
        echo '沈陽(yáng)市,大連市,鐵嶺市,丹東市,錦州市'; 
        break; 
      case '吉林省': 
        echo '長(zhǎng)春市,松原市,吉林市,通化市,四平市'; 
        break; 
    } 
  } 
?> 

關(guān)鍵就是如何實(shí)現(xiàn)AJAX異步交互:參考JS代碼??梢哉f(shuō)的固定步驟。

以上這篇Ajax實(shí)現(xiàn)簡(jiǎn)單下拉選項(xiàng)效果【推薦】就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論