How to achieve non-refresh linkage of drop-down box with Ajax
This time I will show you how to implement non-refresh linkage of drop-down boxes with Ajax. What are the precautions for Ajax to implement non-refresh linkage of drop-down boxes. Here is a practical case, let’s take a look.
HTML code:
@{ Layout = null; } @using DAL; @using System.Data; @{ AreaDal areaDal = new AreaDal(); string areaId = ViewBag.areaId; DataRow drArea = areaDal.GetArea(areaId); string countyId = drArea == null ? "-1" : drArea["countyId"].ToString(); DataRow drCounty = areaDal.GetCounty(countyId); string cityId = drCounty == null ? "-1" : drCounty["cityId"].ToString(); DataRow drCity = areaDal.GetCity(cityId); string provinceId = drCity == null ? "-1" : drCity["provinceId"].ToString(); } <!DOCTYPE html> <html> <head> <title>商圈选择</title> <script type="text/javascript"> //选中 function select(selId, id, callback) { $("#" + selId).val(id); if (callback) callback(); } //获取省列表 function getProvinces(callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetProvinces")", data: {}, success: function (text) { $("#province").html(text); if (callback) callback(); }, error: function () { } }); } //获取市列表 function getCities(pid, callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetCities")", data: { "provinceId": pid, }, success: function (text) { $("#city").html(text); if (callback) callback(); }, error: function () { } }); } //获取区县列表 function getCounties(pid, callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetCounties")", data: { "cityId": pid, }, success: function (text) { $("#county").html(text); if (callback) callback(); }, error: function () { } }); } //获取商圈列表 function getAreas(pid, callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetAreas")", data: { "countyId": pid, }, success: function (text) { $("#area").html(text); if (callback) callback(); }, error: function () { } }); } </script> </head> <body> <select id="province"> <option value="-1">==请选择==</option> </select> <select id="city"> <option value="-1">==请选择==</option> </select> <select id="county"> <option value="-1">==请选择==</option> </select> <select id="area"> <option value="-1">==请选择==</option> </select> <script type="text/javascript"> //选择省 $("#province").change(function () { var id = $(this).find("option:selected").val(); getCities(id, function () { $("#city").change(); }); }); //选择市 $("#city").change(function () { var id = $(this).find("option:selected").val(); getCounties(id, function () { $("#county").change(); }); }); //选择区县 $("#county").change(function () { var id = $(this).find("option:selected").val(); getAreas(id, function () { $("#area").change(); }); }); getProvinces(function () { select("province", '@provinceId', function () { getCities('@provinceId', function () { select("city", '@cityId', function () { getCounties('@cityId', function () { select("county", '@countyId', function () { getAreas('@countyId', function () { select("area", '@areaId'); }); }); }); }); }); }); }); </script> </body> </html>
Controller code:
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Web.Mvc; using DAL; namespace Controllers.Backstage { /// <summary> /// 行政区划 /// </summary> public class AreaController : AdminBaseController { #region 构造函数及变量 private SQLiteHelper.SQLiteHelper sqliteHelper; private AreaDal areaDal; public AreaController() { sqliteHelper = new SQLiteHelper.SQLiteHelper(); areaDal = new AreaDal(); } #endregion #region 行政区划商圈级联选择页面 public ActionResult AreaSelect() { return PartialView(); } #endregion #region 获取全部省 public ActionResult GetProvinces() { DataTable dt = areaDal.GetProvincesAll(); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==请选择==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion #region 根据省获取市 public ActionResult GetCities(string provinceId) { DataTable dt = areaDal.GetCities(provinceId); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==请选择==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion #region 根据市获取区县 public ActionResult GetCounties(string cityId) { DataTable dt = areaDal.GetCounties(cityId); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==请选择==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion #region 根据区县获取商圈 public ActionResult GetAreas(string countyId) { DataTable dt = areaDal.GetAreas(countyId); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==请选择==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion } }
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 jQuery controls dynamic page elements
How to write ajax requests with the highest performance in jQuery
How two zTree interact with each other
The above is the detailed content of How to achieve non-refresh linkage of drop-down box with Ajax. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When you browse the web on your iPhone, the loaded content is temporarily stored as long as the browser app remains open. However, the website updates content regularly, so refreshing the page is an effective way to clear out old data and see the latest published content. This way, you always have the latest information and experiences. If you want to refresh the page on iPhone, the following post will explain you all the methods. How to Refresh Web Pages on Safari [4 Methods] There are several methods to refresh the pages you are viewing on the Safari App on iPhone. Method 1: Use the Refresh Button The easiest way to refresh a page you have open on Safari is to use the Refresh option on your browser's tab bar. If Safa

Is the F5 key not working properly on your Windows 11/10 PC? The F5 key is typically used to refresh the desktop or explorer or reload a web page. However, some of our readers have reported that the F5 key is refreshing their computers and not working properly. How to enable F5 refresh in Windows 11? To refresh your Windows PC, just press the F5 key. On some laptops or desktops, you may need to press the Fn+F5 key combination to complete the refresh operation. Why doesn't F5 refresh work? If pressing the F5 key fails to refresh your computer or you are experiencing issues on Windows 11/10, it may be due to the function keys being locked. Other potential causes include the keyboard or F5 key

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:
