Using jquery's Trigger method to optimize page code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.