<select id="Select1"></select>
<select id="Select2"></select>
<select id="Select3"></select>
<select id="Select4"> </select>
<script src="Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="Scripts/jQuery.CascadingSelect.js" type="text/javascript" charset="gb2312"></script>
$(function() {
$("#Select1").FillOptions("Ajax/AreaHandler.ashx", {
textfield: "RegionName",
valuefiled: "RegionId"
});
$("#Select1").CascadingSelect($("#Select2"), "Ajax/AreaHandler.ashx", {
textfield: "ProvinceName",
valuefiled: "ProvinceId",
parameter: "r"
},
function() {
$ ("# Select2, # select3, # select4 "). empty (). append ('<option> -- select -- </option> '). ATTR ('Disabled ', 'Disabled ');
});
$("#Select2").CascadingSelect($("#Select3"), "Ajax/AreaHandler.ashx", {
textfield: "CityName",
valuefiled: "CityId",
parameter: "p"
},
function() {
$ ("# Select3, # select4 "). empty (). append ('<option> -- select -- </option> '). ATTR ('Disabled ', 'Disabled ');
});
$("#Select3").CascadingSelect($("#Select4"), "Ajax/AreaHandler.ashx", {
textfield: "DistrictName",
valuefiled: "DistrictId",
parameter: "c"
});
JSON format
Background parameters:
if (context.Request.Params["p"] != null)
{
sql = String.Format("SELECT CityId,CityName FROM T_City WHERE ProvinceId= '{0}'", context.Request.Params["p"]);
}
Plug-in (jquery. cascadingselect. JS ):
(Function ($) {$. FN. filloptions = function (URL, options) {Options = $. extend ({textfield: 'text', valuefiled: 'value', selectedindexid: 0, parameter: ''}, options ||{}); $ this = $ (this ); if ($ this. children (). length = 0) {$ this. append ('<option> -- select -- </option> '). ATTR ('Disabled ', 'Disabled');} $ this. empty (). ATTR ('Disabled ', 'Disabled '). append ('<option> loading options </option>'); $. getjson (u RL, function (response) {If (response. count> 0) {$ this. empty (). removeattr ("disabled"); $ this. append ('<option>-select-</option>'); $. each (response. data, function (I, item) {$ this. append ($ ("<option> </option> "). ATTR ("value", Eval ("item. "+ options. valuefiled )). text (eval ("item. "+ options. textfield);} else {$ this. empty (). prop ("disabled", true ). append ('<option> no elements found </option> '); })} $. FN. cascadingselect = function (target, URL, options, endfn) {If (target [0]. tagname! = "Select") throw "target must be select"; if (URL. length = 0) throw "request is required"; if (options. parameter = undefined) throw "parameter is required"; $ (this ). BIND ("change", function () {var newurl = ""; urlstr = URL. split ("? "); Newurl = urlstr [0] + "? "+ Options. parameter + "=" + $ (this ). val (); target. filloptions (newurl, options); If (typeof endfn = "function") {endfn () ;}} $. FN. addoption = function (text, value, selected, index) {Option = New Option (text, value); this [0]. options. add (option, index); this [0]. options [Index]. selected = selected ;}}) (jquery );