jQuery+ajax实现无刷新级联菜单示例

前端之家收集整理的这篇文章主要介绍了jQuery+ajax实现无刷新级联菜单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前台用AJAX直接调用后台方法,老有人发帖提问,没事做个示例

下面是做的一个前台用JQUERY,AJAX调用后台方法做的无刷新级联菜单

CasMenu.aspx页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">

Porschev--<a href="/tag/qiantai/" target="_blank" class="keywords">前台</a>JS(Jquery)<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a><a href="/tag/houtai/" target="_blank" class="keywords">后台</a><a href="/tag/fangfa/" target="_blank" class="keywords">方法</a> 级联<a href="/tag/caidan/" target="_blank" class="keywords">菜单</a>@H_<a href="/tag/301/" target="_blank" class="keywords">301</a>_10@ <script src="js/jquery-1.3.2-vsdoc.js" mce_src="js/jquery-1.3.2-vsdoc.js" type="text/javascript"></mce:script> <script type="text/javascript"> $(function() { $("#selPro").change(function() { //省份下拉<a href="/tag/caidan/" target="_blank" class="keywords">菜单</a>的change事件 var params = '{str:"' + $(this).val() + '"}'; //此处参数名要注意和<a href="/tag/houtai/" target="_blank" class="keywords">后台</a><a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>参数名要一致 $.ajax({ type: "POST",//提交方式 url: "CasMenu.aspx/ShowCity",//提交的<a href="/tag/yemian/" target="_blank" class="keywords">页面</a>/<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>名 data: params,//参数(如果没有参数:null) dataType: "text",//类型 contentType: "application/json; charset=utf-8",beforeSend: function(XMLHttpRequest) { $('#tipsDiv').text("正在<a href="/tag/chaxun/" target="_blank" class="keywords">查询</a>..."); },success: function(msg) { $('#tipsDiv').text("<a href="/tag/chaxun/" target="_blank" class="keywords">查询</a>成功!"); $("#selCity option").each(function() { $(this).remove(); //移除原有项 }); $("<option value='0'>===请选择===</option>").appendTo("#selCity"); //<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>一个默认项 $(eval("(" + msg + ")").d).appendTo("#selCity"); //将返回来的项<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>到下拉<a href="/tag/caidan/" target="_blank" class="keywords">菜单</a>中 },error: function(xhr,msg,e) { alert("error"); } }); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> Porschev--<a href="/tag/qiantai/" target="_blank" class="keywords">前台</a>JS(Jquery)<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a><a href="/tag/houtai/" target="_blank" class="keywords">后台</a><a href="/tag/fangfa/" target="_blank" class="keywords">方法</a> 级联<a href="/tag/caidan/" target="_blank" class="keywords">菜单</a><br /> <br /> <select id="selPro" name="selPro"> <%=strPro %> </select>省(直辖市) <select id="selCity" name="selCity"> <option value="0">===请选择===</option> </select>(市) </div> <div id="tipsDiv"> </div> </form> </body> </html> </pre> </div> <p><h3>CasMenu.aspx.cs</h3></p> <div class="jb51code"> <pre class="brush:csharp;"> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CasMenuModels; using CasMenuBLL; using System.Text; public partial class _Default : System.Web.UI.Page { public static string strPro = string.Empty; //省份下拉项 public static string strCity = string.Empty; //城市下拉项 protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { } ShowPro(); } <h1>region##省份下拉列表框</h1> <p>/// <summary><br /> /// 省份下拉列表框<br /> /// </summary><br /> /// <returns></returns><br /> public string ShowPro()<br /> {<br /> StringBuilder str = new StringBuilder(); //得到所有省份集合<br /> List<CasMenuModels.Province> list = new CasMenuBLL.ProvinceManager().GetAllProvince(); //<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>一个初始项<br /> str.Append("<option value=\"");<br /> str.Append("0");<br /> str.Append("\">");<br /> str.Append("===请选择===");<br /> str.Append("</option>"); </p> <pre><code>//循环追加省份下拉项 foreach (CasMenuModels.Province p in list) { str.Append("<option value=\""); str.Append(p.ProvinceId); str.Append("\"&gt;"); str.Append(p.ProvinceName); str.Append("</option>"); } return strPro = str.ToString(); </code></pre> <p>} </p> <h1>endregion #region##城市下拉列表框</h1> <p>/// <summary><br /> /// 城市下拉列表框<br /> /// </summary><br /> /// <param name="str">省份ID</param><br /> /// <returns></returns><br /> [System.Web.Services.WebMethod()]<br /> public static string ShowCity(string str) //<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>是静态的<br /> {<br /> StringBuilder strCi = new StringBuilder();<br /> if (str == "0") //为初始项<br /> {<br /> strCi.Append("<option value=\"");<br /> strCi.Append("请选择");<br /> strCi.Append("\">");<br /> strCi.Append("请选择");<br /> strCi.Append("</option>");<br /> }<br /> else<br /> {<br /> List<CasMenuModels.City> list = new CasMenuBLL.CityManager().GetAllByProId(Convert.ToInt32(str)); //根据省份ID得到城市集合<br /> foreach (City c in list)<br /> {<br /> strCi.Append("<option value=\"");<br /> strCi.Append(c.CityId);<br /> strCi.Append("\">");<br /> strCi.Append(c.CityName);<br /> strCi.Append("</option>");<br /> }<br /> }<br /> return strCity = strCi.ToString();<br /> } </p> <h1>endregion</h1> <p>} </p> </pre> </div> <p>要注意的地方我都有注释,</p> <p>当然<a href="/tag/qiantai/" target="_blank" class="keywords">前台</a>调入<a href="/tag/houtai/" target="_blank" class="keywords">后台</a><a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>远不止这一种,比如还有AJAXPRO也很好用</p> <p>以上所述就是本文的全部<a href="/tag/neirong/" target="_blank" class="keywords">内容</a>了,希望大家能够喜欢。</p></div> <div class="topcard-tags"><a href="/tag/ajaxp/" class="tag_link" target="_blank">ajax</a><a href="/tag/jQuery/" class="tag_link" target="_blank">jQuery</a><a href="/tag/wushuaxinjiliancaidan/" class="tag_link" target="_blank">无刷新级联菜单</a></div> <ul class="list-group"> <li class="list-group-item"><a href="/ajax/54239.html" title="jQuery结合ajax实现动态加载文本内容">上一篇:jQuery结合ajax实现动态加载文本内</a><a href="/ajax/53972.html" title="jQuery取消ajax请求的方法" class="text-muted pull-right">下一篇:jQuery取消ajax请求的方法</a> </li> </ul> </div> </div> </div> <!-- row end --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4605373693034661" data-ad-slot="9144498553"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> </div> </div> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <div class="title"><h1>猜你在找的Ajax相关文章</h1></div> <div class="list_con"> <a href="/ajax/995195.html" title="网页的异步请求(Ajax)"><div class="title">网页的异步请求(Ajax)</div> <div class="summary">JS原生Ajax操作(XMLHttpRequest) GET请求 POST请求 兼容性问题 利用iframe模拟ajax 实现表...</div> <time class="summary">作者:前端之家 时间:2021-01-14</time> </a> </div> <div class="list_con"> <a href="/ajax/992908.html" title="手动实现AJAX"><div class="title">手动实现AJAX</div> <div class="summary">AJAX 每日更新前端基础,如果觉得不错,点个star吧 &#128515; https://github.com/Win...</div> <time class="summary">作者:前端之家 时间:2020-12-19</time> </a> </div> <div class="list_con"> <a href="/ajax/992023.html" title="axios异步提交表单数据的不同形式"><img class="lazy" src="/images/np.jpg" data-original="/res/2020/12-12/21/2c69f9c8b1929806fd861e6309bec705.png" title="" width="160" height="90" style="float:right;margin-left:30px;display:none;" /><div class="title">axios异步提交表单数据的不同形式</div> <div class="summary">踩坑Axios提交form表单几种格式 前后端分离的开发前后端, 前端使用的vue,后端的安全模块使...</div> <time class="summary">作者:前端之家 时间:2020-12-12</time> </a> </div> <div class="list_con"> <a href="/ajax/881610.html" title="php的ajax简单实例"><div class="title">php的ajax简单实例</div> <div class="summary">很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈...</div> <time class="summary">作者:前端之家 时间:2020-11-11</time> </a> </div> <div class="list_con"> <a href="/ajax/881257.html" title="Json与Ajax(注册实例)"><div class="title">Json与Ajax(注册实例)</div> <div class="summary">需要在服务器上进行哈 jquery的ajax方法: // jquery请求 $.ajax({ url: &quot;./serv...</div> <time class="summary">作者:前端之家 时间:2020-11-07</time> </a> </div> <div style="border-bottom: 1px solid #f4f4f4;margin-top:20px;"> <ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-fr-2o+fp-dx-wx" data-ad-client="ca-pub-4605373693034661" data-ad-slot="4561116489"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div><div class="list_con"> <a href="/ajax/881219.html" title="jSon和Ajax登录功能,ajax数据交互案例"><div class="title">jSon和Ajax登录功能,ajax数据交互案例</div> <div class="summary">ajax实例,检测用户与注册 检测用户名是否被占用: 在用户填写完用户名之后,ajax会异步向...</div> <time class="summary">作者:前端之家 时间:2020-11-07</time> </a> </div> <div class="list_con"> <a href="/ajax/880192.html" title="关于AjaxControlToolkit的样式问题"><div class="title">关于AjaxControlToolkit的样式问题</div> <div class="summary">网(LieHuo.Net)教程 技术文章一直都是转载的,从来没有自创过.终于遇到AjaxControlToolkit,...</div> <time class="summary">作者:前端之家 时间:2020-11-04</time> </a> </div> <div class="list_con"> <a href="/ajax/880189.html" title="一个简单的AJAX实现(HELLO AJAX)"><div class="title">一个简单的AJAX实现(HELLO AJAX)</div> <div class="summary">客户端部分: <html> <head> <meta http-equiv="Content-Type" ...</div> <time class="summary">作者:前端之家 时间:2020-11-04</time> </a> </div> <div class="list_con"> <a href="/ajax/880188.html" title="AJAX中文乱码的两类问题及解决之策"><div class="title">AJAX中文乱码的两类问题及解决之策</div> <div class="summary">1)发送路径中的参数有中文,在服务器段接收参数值是乱码 例如: var url=”a.jsp?na...</div> <time class="summary">作者:前端之家 时间:2020-11-04</time> </a> </div> <div class="list_con"> <a href="/ajax/880154.html" title="实例:AJAX返回xml代码如何在网页中显示"><div class="title">实例:AJAX返回xml代码如何在网页中显示</div> <div class="summary">建站学院(LieHuo.Net)文档 今天来学习一篇实例代码教程:AJAX返回xml代码如何在网页中显...</div> <time class="summary">作者:前端之家 时间:2020-11-04</time> </a> </div> <div style="border-bottom: 1px solid #f4f4f4;margin-top:20px;"> <ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-fr-2o+fp-dx-wx" data-ad-client="ca-pub-4605373693034661" data-ad-slot="4561116489"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></div> </div> </div> </div> <!-- left end--> <!-- right --> <div class="col-sm-12 col-md-12 col-lg-3"> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">编程分类</label> <div class="cate mt-20"><a href="/html/" title="HTML">HTML</a><a href="/html5/" title="HTML5">HTML5</a><a href="/js/" title="JavaScript">JavaScript</a><a href="/css/" title="CSS">CSS</a><a href="/jquery/" title="jQuery">jQuery</a><a href="/bootstrap/" title="Bootstrap">Bootstrap</a><a href="/angularjs/" title="Angularjs">Angularjs</a><a href="/typescript/" title="TypeScript">TypeScript</a><a href="/vue/" title="Vue">Vue</a><a href="/dojo/" title="Dojo">Dojo</a><a href="/json/" title="Json">Json</a><a href="/electron/" title="Electron">Electron</a><a href="/nodejs/" title="Node.js">Node.js</a><a href="/extjs/" title="extjs">extjs</a><a href="/express/" title="Express ">Express </a><a href="/xml/" title="XML">XML</a><a href="/es6/" title="ES6">ES6</a><a href="/ajax/" title="Ajax">Ajax</a><a href="/flash/" title="Flash">Flash</a><a href="/unity/" title="Unity">Unity</a><a href="/react/" title="React">React</a><a href="/flex/" title="Flex">Flex</a><a href="/antdesign/" title="Ant Design">Ant Design</a><a href="/webfrontend/" title="Web前端">Web前端</a><a href="/weapp/" title="微信小程序">微信小程序</a><a href="/wxmp/" title="微信公众号">微信公众号</a><div class="clearfix"></div> </div> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <!-- f2er-rightads --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4605373693034661" data-ad-slot="7756441254" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">最新文章</label> <ul class="n-list"><li><a href="/ajax/995195.html" title="网页的异步请求(Ajax)" target="_blank">• 网页的异步请求(Ajax)</a></li> <li><a href="/ajax/992908.html" title="手动实现AJAX" target="_blank">• 手动实现AJAX</a></li> <li><a href="/ajax/992023.html" title="axios异步提交表单数据的不同形式" target="_blank">• axios异步提交表单数据的不</a></li> <li><a href="/ajax/881610.html" title="php的ajax简单实例" target="_blank">• php的ajax简单实例</a></li> <li><a href="/ajax/881257.html" title="Json与Ajax(注册实例)" target="_blank">• Json与Ajax(注册实例)</a></li> <li><a href="/ajax/881234.html" title="Ajax基础原理与应用" target="_blank">• Ajax基础原理与应用</a></li> <li><a href="/ajax/881219.html" title="jSon和Ajax登录功能,ajax数据交互案例" target="_blank">• jSon和Ajax登录功能,ajax</a></li> <li><a href="/ajax/880192.html" title="关于AjaxControlToolkit的样式问题" target="_blank">• 关于AjaxControlToolkit的</a></li> <li><a href="/ajax/880189.html" title="一个简单的AJAX实现(HELLO AJAX)" target="_blank">• 一个简单的AJAX实现(HELLO</a></li> <li><a href="/ajax/880188.html" title="AJAX中文乱码的两类问题及解决之策" target="_blank">• AJAX中文乱码的两类问题及</a></li> </ul> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">热门标签 <span class="pull-right tx-12"> <a href="/all" target="_blank">更多 ►</a></span> </label> <div class="topcard-tags"><a href="/tag/guanbiyangao/" title="关闭广告" target="_blank">关闭广告</a><a href="/tag/danduheaders/" title="单独headers" target="_blank">单独headers</a><a href="/tag/fengzhuangdaima/" title="封装代码" target="_blank">封装代码</a><a href="/tag/tishicuowu/" title="提示错误" target="_blank">提示错误</a><a href="/tag/zhengshuzhengze/" title="整数正则" target="_blank">整数正则</a><a href="/tag/fei0kaitou/" title="非0开头" target="_blank">非0开头</a><a href="/tag/tiaoye/" title="跳页" target="_blank">跳页</a><a href="/tag/chuyema/" title="出页码" target="_blank">出页码</a><a href="/tag/antdtable/" title="antd table" target="_blank">antd table</a><a href="/tag/tishiURLweizhuce/" title="提示URL未注册" target="_blank">提示URL未注册</a><a href="/tag/gongzhonghaozhifu/" title="公众号支付" target="_blank">公众号支付</a><a href="/tag/vuehashmoshi/" title="vue hash模式" target="_blank">vue hash模式</a><a href="/tag/iSlider/" title="iSlider" target="_blank">iSlider</a><a href="/tag/chepaijianpan/" title="车牌键盘" target="_blank">车牌键盘</a><a href="/tag/xunhuantupian/" title="循环图片" target="_blank">循环图片</a><a href="/tag/echartsshuangzhexian/" title="echarts 双折线" target="_blank">echarts 双折</a><a href="/tag/zuoyoubuju/" title="左右布局" target="_blank">左右布局</a><a href="/tag/DllPlugin/" title="DllPlugin" target="_blank">DllPlugin</a><a href="/tag/duixiangchuangjian/" title="对象创建" target="_blank">对象创建</a><a href="/tag/daziyouxi/" title="打字游戏" target="_blank">打字游戏</a><a href="/tag/quanxuan/" title="圈选" target="_blank">圈选</a><a href="/tag/lianglan/" title="两栏" target="_blank">两栏</a><a href="/tag/yunhanshu/" title="云函数" target="_blank">云函数</a><a href="/tag/mengban/" title="蒙版" target="_blank">蒙版</a><a href="/tag/ES2020/" title="ES2020" target="_blank">ES2020</a><a href="/tag/chuchuang/" title="橱窗" target="_blank">橱窗</a><a href="/tag/wufenggundonglunbo/" title="无缝滚动轮播" target="_blank">无缝滚动轮播</a><a href="/tag/sekuaipengzhuang/" title="色块碰撞" target="_blank">色块碰撞</a><a href="/tag/zujianxiaohui/" title="组件销毁" target="_blank">组件销毁</a><a href="/tag/wendangcaozuo/" title="文档操作" target="_blank">文档操作</a></div> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <!-- f2er-rightads --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4605373693034661" data-ad-slot="7756441254" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <!-- row end --> </div> <!-- right end --> </div> </div> <footer id="footer"> <div class="container"> <div class="row hidden-xs"> <dl class="col-sm-6 site-link"> <dt>最近更新</dt><dd><a href="/win11/1005328.html" title="小米手机重装系统价格多少?专业维修服务详解" target="_blank">· 小米手机重装系统价格多少?专业维修服务详解</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005327.html" title="手把手教你重装电脑系统,让你的电脑焕然一新!" target="_blank">· 手把手教你重装电脑系统,让你的电脑焕然一新!</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005326.html" title="教你一步步重装XP系统,让你的电脑重获新生" target="_blank">· 教你一步步重装XP系统,让你的电脑重获新生</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005325.html" title="从备份到上网:一步步教你重装电脑系统" target="_blank">· 从备份到上网:一步步教你重装电脑系统</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005324.html" title="Sony笔记本电脑一键重装系统详细图文教程" target="_blank">· Sony笔记本电脑一键重装系统详细图文教程</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005323.html" title="Lenovo笔记本重装系统超详细教程,小白也能轻松上手" target="_blank">· Lenovo笔记本重装系统超详细教程,小白也能轻松...</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005322.html" title="联想笔记本一键重装Win10系统详细教程" target="_blank">· 联想笔记本一键重装Win10系统详细教程</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005321.html" title="电脑系统故障无需愁,专业维修店帮你重装旧貌换新颜" target="_blank">· 电脑系统故障无需愁,专业维修店帮你重装旧貌换新...</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005320.html" title="联想笔记本一键重装Win10系统图文教程,小白也能轻松搞定!" target="_blank">· 联想笔记本一键重装Win10系统图文教程,小白也能...</a><span class="text-muted pull-right">02-05</span></dd> <dd><a href="/win11/1005319.html" title="笔记本重装系统图文教程:从光盘启动一步到位" target="_blank">· 笔记本重装系统图文教程:从光盘启动一步到位</a><span class="text-muted pull-right">02-05</span></dd> </dl> <dl class="col-sm-4 site-link"> <dt>好站推荐</dt><dd> <a href="https://www.runoob.com" title="菜鸟教程(www.runoob.com)提供了编程的基础技术教程, 介绍了HTML、CSS、Javascript、Python,Java,Ruby,C,PHP , MySQL等各种编程语言的基础知识。 同时本站中也提供了大量的在线实例,通过实例,您可以更好的学习编程。" target="_blank">菜鸟教程</a></dd><dd> <a href="https://www.jb51.cc" title="编程之家(www.jb51.cc)是成立于2017年面向全球中文开发者的技术内容分享平台。提供编程导航、编程问答、编程博文、编程百科、编程教程、编程工具、编程实例等开发者最需要的编程技术内容与开发工具支持,与你一起学习编程,相信编程改变未来!" target="_blank">编程之家</a></dd><dd> <a href="" title="前端之家 f2er.com 前端开发人员所需学习知识手册。" target="_blank">前端之家</a></dd></dl> <dl class="col-sm-2 site-link"> <dt>商务合作</dt> <dd><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=76874919&site=qq&menu=yes">联系我们</a></dd> </dl> </div> <div class="copyright"> Copyright © 2019 前端之家. 当前版本 V7.0.16<br> <span class="ml5">前端之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-10</a></span> </div> </div> </footer> <script type="text/javascript" src="/js/base.js"></script> </body> </html>