ajax實(shí)現(xiàn)select三級聯(lián)動效果
本文利用ajax技術(shù)從數(shù)據(jù)庫中獲取數(shù)據(jù),動態(tài)實(shí)現(xiàn)select三級聯(lián)動,效果圖如下:
前端js代碼如下:
<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字符串轉(zhuǎn)換為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字符串轉(zhuǎn)換為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字符串轉(zhuǎn)換為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); } }); }
部分html代碼
<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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ajax+smarty技術(shù)實(shí)現(xiàn)無刷新分頁
這篇文章主要介紹了Ajax+smarty技術(shù)實(shí)現(xiàn)無刷新分頁的相關(guān)資料,需要的朋友可以參考下2016-03-03SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)
這篇文章主要介紹了SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-05-05基于Ajax和forms組件實(shí)現(xiàn)注冊功能的實(shí)例代碼
本文通過實(shí)例代碼給大家分享了基于Ajax和forms組件實(shí)現(xiàn)注冊功能,需要的朋友可以參考下2018-02-02ajax實(shí)現(xiàn)數(shù)據(jù)刪除、查看詳情功能
本文主要介紹了ajax實(shí)現(xiàn)數(shù)據(jù)刪除、查看詳情功能,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Ajax與mysql數(shù)據(jù)交互實(shí)現(xiàn)留言板功能
最近自己做了一個小demo,實(shí)現(xiàn)了Ajax與MySQL的數(shù)據(jù)交互,js部分用的是jq,后臺用的是PHP,數(shù)據(jù)庫是mysql,下面通過本文給大家詳細(xì)介紹下2016-12-12