Home  >  Article  >  Web Front-end  >  Use ajax to implement select three-level linkage function

Use ajax to implement select three-level linkage function

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 15:26:292009browse

This time I will bring you the use of ajax to implement the select three-level linkage function, and what are the precautions for using ajax to implement the select three-level linkage function. The following is a practical case, let's take a look.

This article uses ajax technology to obtain data from the database and dynamically implement select three-level linkage. The rendering is as follows:

The front-end js code is as follows:

<script type="text/javascript" src="jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type:"get",
dataType:"text",
url:"GetItemAction?itemlevel=1",
 success:function(data){
 // 由JSON字符串转换为JSON对象
  var objJSON=eval("("+data+")");
 var len=objJSON.itemInfo.length; 
 var objSelect=document.getElementById("firstlevel");
 for(var i=0;i<len;i++){
 var op=new Option(objJSON.itemInfo[i].itemname,objJSON.itemInfo[i].itemcode);
 objSelect.add(op);
 } 
 },
error: function (XMLHttpRequest, textStatus, errorThrown) { 
   alert(errorThrown); 
  }
});
});
//显示二级分类
function firstlevel_Click(){
var objfirst=document.getElementById("firstlevel");
var index=objfirst.selectedIndex; //选中索引 
var itemcode=objfirst.options[index].value; //选中值
if(itemcode==0){
alert("请选择事件类别");
return;
}
// 删除二级分类保留第一个
var objsecond=document.getElementById("secondlevel");
for(var i=objsecond.options.length-1;i>0;i--){
objsecond.options.remove(i);
}
// 删除三级分类保留第一个
var objthird=document.getElementById("thirdlevel");
for(var i=objthird.options.length-1;i>0;i--){
objthird.options.remove(i);
}
$.ajax({
type:"get",
dataType:"text",
url:"GetItemAction?itemlevel=2&itemcode="+itemcode,
 success:function(data){
 // 由JSON字符串转换为JSON对象
  var objJSON=eval("("+data+")");
 var len=objJSON.itemInfo.length;
 var objSelect=document.getElementById("secondlevel");
 for(var i=0;i<len;i++){
 var op=new Option(objJSON.itemInfo[i].itemname,objJSON.itemInfo[i].itemcode);
 objSelect.add(op);
 } 
 },
error: function (XMLHttpRequest, textStatus, errorThrown) { 
   alert(errorThrown); 
  }
});
}
//显示三级分类
function secondlevel_Click(){
var objsecond=document.getElementById("secondlevel");
var index=objsecond.selectedIndex; //选中索引 
var itemcode=objsecond.options[index].value; //选中值
if(itemcode==0){
alert("请选择事件类别");
return;
}
// 删除三级分类保留第一个
var objthird=document.getElementById("thirdlevel");
for(var i=objthird.options.length-1;i>0;i--){
objthird.options.remove(i);
}
$.ajax({
type:"get",
dataType:"text",
url:"GetItemAction?itemlevel=3&itemcode="+itemcode,
 success:function(data){
 // 由JSON字符串转换为JSON对象
  var objJSON=eval("("+data+")");
 var len=objJSON.itemInfo.length;
 var objSelect=document.getElementById("thirdlevel");
 for(var i=0;i<len;i++){
 var op=new Option(objJSON.itemInfo[i].itemname,objJSON.itemInfo[i].itemcode);
 objSelect.add(op);
 } 
 },
error: function (XMLHttpRequest, textStatus, errorThrown) { 
   alert(errorThrown); 
  }
});
}

Part of the html code

<table class="table-exposure">
 <tr>
 <td height="40">
 <select id="firstlevel" onchange="firstlevel_Click()" style="width:99%;font-size:1em;">
 <option value="0">请选择事件大类</option>
 </select>     
 </td>
 </tr>
 <tr>
 <td height="40">
 <select id="secondlevel" onchange="secondlevel_Click()" style="width:99%;font-size:1em;">
 <option value="0">请选择事件小类</option>
 </select>
 </td>
 </tr>
 <tr>
 <td height="40">
 <select id="thirdlevel" style="width:99%;font-size:1em;">
 <option value="0">请选择事件类别</option>
 </select>
 </td>
 </tr>
 </table>
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:

Steps to implement WeChat web page authorized login using ajax (with code)

Determine ID card How to write the regular format of bank card number

The above is the detailed content of Use ajax to implement select three-level linkage function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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