<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CasMenu.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Porschev--前台JS(Jquery)調用後台方法 串聯功能表</title>
<script src="http://www.cnblogs.com/Javascript/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#selectShop").change(function() { //省份下拉式功能表的change事件
var params = '{str:"' + $(this).val() + '"}'; //此處參數名要注意和後台方法參數名要一致
$.ajax({
type: "POST", //提交方式
url: "List.aspx/ShowShopPro", //提交的頁面/方法名
data: params, //參數(如果沒有參數:null)
dataType: "text", //類型
contentType: "application/json; charset=utf-8",
beforeSend: function(XMLHttpRequest) {
$('#tipsDiv').text("正在查詢...");
},
success: function(msg) {
$('#tipsDiv').text("查詢成功!");
$("#selectShopPro option").each(function() {
$(this).remove(); //移除原有項
});
$("<option value='0'>===請選擇===</option>").appendTo("#selectShopPro"); //添加一個預設項
$(eval("(" + msg + ")").d).appendTo("#selectShopPro"); //將返回來的項添加到下拉式功能表中
},
error: function(xhr, msg, e) {
alert("error");
}
});
});
});
var i = 1;
//複製添加
function adds() {
$("#selectShop").clone().prependTo("#tipsDiv");
$("#tipsDiv select").attr("id","select"+i+"");
alert($("#tipsDiv").html());
i++;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Porschev--前台JS(Jquery)調用後台方法 串聯功能表<br />
<br />
<select id="selectShop">
<%=strShop%>
</select>
<select id="selectShopPro" name="selCity">
<option value="0">===請選擇===</option>
</select>
<div id="tipsDiv"></div>
<a href="javascript:adds()">添加</a>
</div>
</form>
</body>
</html>
CasMenu.aspx.cs 檔案中
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
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowPro ();
}
}
public static string strShop = string.Empty;
public static string strShopPro = string.Empty;
public string ShowPro()
{
StringBuilder str = new StringBuilder();
List<ZGWed.Model.Shop> list = new ShopBLL().GetALLItems();
//添加一個初始項
str.Append("<option value=\"");
str.Append("0");
str.Append("\">");
str.Append("===請選擇===");
str.Append("</option>");
//迴圈追加省份下拉項
foreach (ZGWed.Model.Shop s in list)
{
str.Append("<option value=\"");
str.Append(s.ID);
str.Append("\">");
str.Append(s.ShopName);
str.Append("</option>");
}
return strShop = str.ToString();
}
[System.Web.Services.WebMethod()]
public static string ShowShopPro(string str)
{
StringBuilder strCi = new StringBuilder();
if (str == "0") //為初始項
{
strCi.Append("<option value=\"");
strCi.Append("請選擇");
strCi.Append("\">");
strCi.Append("請選擇");
strCi.Append("</option>");
}
else
{
List<string> scon = new List<string>();
scon.Add("shopid,"+str+",=");
List<ZGWed.Model.ShopProductInfo> list = new ShopProductInfoBLL().GetItemsByCondition(scon) ;
foreach (ZGWed.Model.ShopProductInfo c in list)
{
strCi.Append("<option value=\"");
strCi.Append(c.ID);
strCi.Append("\">");
strCi.Append(c.ShopID);
strCi.Append("</option>");
}
}
return strShopPro = strCi.ToString();
}
#endregion
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/porschev/archive/2010/10/15/5943579.aspx