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

How to achieve non-refresh linkage of drop-down box with Ajax

php中世界最好的语言
Release: 2018-03-15 16:07:12
Original
1451 people have browsed it

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();
}



  商圈选择
  

              
Copy after login

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
{
  /// 
  /// 行政区划
  /// 
  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("");
      foreach (DataRow dr in dt.Rows)
      {
        sbHtml.AppendFormat("", 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("");
      foreach (DataRow dr in dt.Rows)
      {
        sbHtml.AppendFormat("", 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("");
      foreach (DataRow dr in dt.Rows)
      {
        sbHtml.AppendFormat("", 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("");
      foreach (DataRow dr in dt.Rows)
      {
        sbHtml.AppendFormat("", dr["id"].ToString(), dr["name"].ToString());
      }
      return Content(sbHtml.ToString());
    }
    #endregion
  }
}
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 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!

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!