jquery Plug-in Extension instance "Add callback function" _jquery

Source: Internet
Author: User

This example describes the method of the jquery plug-in extension. Share to everyone for your reference, specific as follows:

<script language= "javascript" type= "Text/javascript" >
function dosomething (callback) {
  //... 
  Call the callback
  callback (' Stuff ', ' goes ', ' here ');//Give callback assign value, callback is a function variable
} functions
foo1 (A, B, c) {
  //I ' m The callback
  alert (A + "" + B + "" + C);
}
DoSomething (FOO1);  The FOO1 function will use the data in the callback function stuff goes here
var foo2 = function (a,b,c) {
  //I ' m The callback
  alert (A + "" + B + "" + C);
}
DoSomething (FOO2); The FOO2 function uses the data in the callback function to stuff goes here
dosomething (function (a,b,c) {
  alert (A + "" + B + "" + C); function functions will use the data in the callback function stuff goes here
});
</script>

Callback This parameter must be a function to be valid. Can play a role in callback.

<script language= "javascript" type= "Text/javascript" >
function dosomething (callback) {
  //... 
  Call the callback
  if (typeof callback = = ' function ') {
    callback (' Stuff ', ' goes ', ' here ');/= assign value to callback Callback is a function variable
  }else{
    alert (' jb51.net ');
  }
function Foo1 (A, B, c) {
  //I ' m The callback
  alert (A + "" + B + "" + C);
}
DoSomething (FOO1);  The FOO1 function will use the data in the callback function stuff goes here
var foo2 = function (a,b,c) {
  //I ' m The callback
  alert (A + "" + B + "" + C);
}
DoSomething (FOO2); The FOO2 function uses the data in the callback function to stuff goes here
dosomething (function (a,b,c) {
  alert (A + "" + B + "" + C); function functions will use the data in the callback function stuff goes here
});
var Foo3 = ' a ';
DoSomething (FOO3);
</script>

Foo3 is not a function when the pop-up jb51.net

jquery instances

Original function

$.fn.cityselect=function (Settings)

Add callback

$.fn.cityselect=function (settings, Changehandle)//Add callback function Changehandle

Assign a value to a callback function

Option variable Assignment event
var selectchange = function (areatype) { 
  if (typeof changehandle = = ' function ') {//Judge whether callback is a function C3/>var prov_id = prov_obj.get (0). SelectedIndex;
    var city_id = city_obj.get (0). SelectedIndex;
    var dist_id = dist_obj.get (0). SelectedIndex;
    if (!settings.required) {
      prov_id--;
      city_id--;
      dist_id--;
    };
    if (dist_id<0) {
      var data = {
        PROV:CITY_JSON.CITYLIST[PROV_ID].P,
        city:city_json.citylist[prov_id ].C[CITY_ID].N,
        dist:null
      };
    else{
      var data = {
        PROV:CITY_JSON.CITYLIST[PROV_ID].P,
        city:city_json.citylist[prov_id].c[city_id] . N,
        Dist:city_json.citylist[prov_id].c[city_id].a[dist_id].s
      };
    Changehandle (data, areatype); Returns two processed data
  }
};

Get data from the provinces and counties and trigger the Change event type Areatype

Event
prov_obj.bind ("Change", function () {
    citystart ()) When the province is selected;
    Selectchange (' Prov '); Return Data
});
Event
city_obj.bind ("Change", function () {
    diststart ()) when the city level is selected;
    Selectchange (' City '); Return Data
});
Event
dist_obj.bind ("Change", function () {
    selectchange (' dist ');//Return Data}) occurs when the zone level is selected
;

Executing in individual events

Front-End use

$ ("#s_city"). Cityselect ({
  prov: "Jiangsu Province", City
  : "Suqian",
  Dist: "Sucheng District",
  nodata: "None"
},
function (data, type) {
  selectagent (data.city, data.dist);
});

Use the callback back to the data, used in the Selectagent function

function Selectagent (city,district) {
    $.ajax ({
      type: "POST",
      URL: "{sh::u (' Index/ajax '), array (' Todo ' = > ' getagent ')} ",
      data:" city= "+city+" &district= "+district,
      success:function (JSON) {
        JSON = Json.parse (JSON);
        Opt_str = "<option value= ' >-Please select-</option>"
        if (Json.status = 1) {
          $.each ( Index,con) {
            opt_str = "<option value=" +con.id+ ">" +con.name+ "Tel:" +con.tel+ "</option>"
          })
        }
        $ (' #agent_id '). HTML (OPT_STR);
      }
    );


Go to Ajax to get the appropriate agent data.

Retrofit plugin complete.

For more information on jquery-related content readers can view the site topics: "jquery common Plug-ins and Usage summary", "JQuery Extended Skills Summary", "jquery switching effects and Skills summary", "jquery traversal algorithm and Skills summary", " jquery Common Classic Effects Summary "jquery animation and special effects usage Summary" and "jquery Selector usage Summary"

I hope this article will help you with the jquery program design.

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.