Home > Web Front-end > JS Tutorial > body text

How to use JS+AJAX to make three-level linkage

php中世界最好的语言
Release: 2018-06-01 11:19:34
Original
1423 people have browsed it

This time I will show you how to use JS AJAX to make three-level linkage, and what are the precautions for using JS AJAX to make three-level linkage. The following is a practical case, let's take a look.

js The implementation code of three-level linkage is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>js原生ajax</title>
</head>
<body>
  <select name="sel1">
    <option value="" >-请选择 省/直辖市/自治区-</option>
  </select>
  <select name="sel2">
    <option value="" >-请选择 市-</option>
  </select>
  <input type="text" value="" id="int"/>
  <script>
    var sel1 = document.getElementsByName('sel1')[0];
    var sel2 = document.getElementsByName('sel2')[0];
    var ints = document.getElementById('int');
    // 创建请求对象
    var a = new XMLHttpRequest();
    // 初始化
    a.open('get','city.json','true');
    // 发送
    a.send();
    //readySate 状态码 交互进行到了哪一步
    //0:请求未初始化
    //1:服务器链接已建立
    //2:请求已经接受
    //3:请求处理中
    //4:请求已经完成,且响应已就绪
    //status 交互是否成功
    a.onreadystatechange = function(){
      if(a.status ==200||a.status == 304){
        if(a.readyState == 4){
          var obj = JSON.parse(a.response);//responseText:获得字符串形式的响应数据。
          var b = obj.城市代码;
          for(var i = 0;i<b.length;i++){
            var nOpt = document.createElement(&#39;option&#39;);
            var nOpt_t =document.createTextNode(b[i].省);
            nOpt.appendChild(nOpt_t);
            sel1.appendChild(nOpt);
            nOpt.value = i;
            console.log(ints.value)
          }
          sel1.onchange = function (){
            var index = sel1.selectedIndex;  //获取select选择的option的下标值
            var va = sel1.options[index].value//获取select第几个option的value值
            var city = b[va].市;    //获取他下边的市
            sel2.options.length = 1;  //清空所有的select下的option的值
            for(var i = 0;i<city.length;i++){
              var nOpt = document.createElement(&#39;option&#39;);
              var nOpt_t =document.createTextNode(city[i].市名);
              nOpt.appendChild(nOpt_t);
              sel2.appendChild(nOpt);
              nOpt.value = i;
              ints.value = "";
            }
          }
          sel2.onchange = function (){
            var sel1v = sel1.value;
            var sel2v = sel2.value;
            var intsi = b[sel1v][&#39;市&#39;][sel2v][&#39;编码&#39;];
            ints.value = intsi;
          }
        }
      }
    }
  </script>
</body>
</html>
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use JS CSS3 to implement image zooming in and out in response to mouse movement

How to use Vue combined with Video. js plays m3u8 video

The above is the detailed content of How to use JS+AJAX to make three-level linkage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!