When we do page cascading, we often use AJAX to process data and write a change event for the drop-down menu.
City and Region linkage $ ("#City"). Change (function () { var cityvalue = $ ("#City"). Val (); if (Cityvalue = =-1) { return; } $.ajax ({ URL: "/bi/getareainfo", Type: "GET", data: {id:cityvalue}, timeout:5000, async: False, DataType: "JSON", success:function (Result) { $ ("#Area"). empty (); $ ("#Area"). Append ("<option value= '-1 ' > Please select Region </option>"); for (var i = 0; I < result. Areainfo.length; i++) { $ ("<option value= '" + result.) Areainfo[i].id + "' >" + result. Areainfo[i]. Name + "</option>"). AppendTo (' #Area ');}} ) ;
When editing functions, if cascade information is involved, we will assign the drop-down box when binding the data: (the following code is usually placed at the end of the page)
<input type= "hidden" value= "@Model. Areaid" id= "Hdareaid"/>
<input type= "hidden" value= "@Model. Cityid" id= "Hdcityid"/>
<script type= "Text/javascript" >
var Areaid = $ ("#hdAreaID"). Val ();
var Cityid = $ ("#hdCityID"). Val ();
$ ("#City"). Val (Cityid);
$.ajax ({
URL: "/bi/getareainfo",
Type: "GET",
Data: {Id:cityid},
timeout:5000,
Async:false,
DataType: "JSON",
Success:function (Result) {
$ ("#Area"). empty ();
$ ("#Area"). Append ("<option value= '-1 ' > Please select Region </option>");
for (var i = 0; I < result. Areainfo.length; i++) {
$ ("<option value=" + result.) Areainfo[i].id + "' >" + result. Areainfo[i]. Name + "</option>"). AppendTo (' #Area ');
}
$ ("#Area"). Val (Areaid);
}
});
</script>
So the question comes, the repetition of the code is a lot of it, this time we can use the JQuery trigger method, simulation of automatic triggering, can achieve the same effect.
$ (function () {
$ ("#City"). Val ("@Model. Cityid");
$ ("#City"). Trigger ("Change", "@Model. Cityid");
$ ("#Area"). Val ("@Model. Areaid");
$ ("#Area"). Trigger ("Change", "@Model. Areaid");
});
Code streamlined a lot ha.
Using jquery's Trigger method to optimize page code