How to pass parameters such as click (), change () in jquery to implement _jquery

Source: Internet
Author: User
Because to do such a job, the two select option is converted to each other, as shown in the following diagram:

This job is to add the click () event to several buttons, which is the general usage:
Copy Code code as follows:

$ ("#but_one"). Click (function () {
$ ("#select1 option:selected"). Appendto ($ ("#select2"));
});

Then I looked up the official document, the description of click Is this, then I still do not have Baidu to answer,
I thought about the reusability of the code and wanted to go directly to the "Select1" and "Select2" strings, and I used the following method:
Copy Code code as follows:

$ ("#but_one"). Click (SELECT ("Select1", "Select2"));
Improve code reusability, based on function changes
function Select (S1,S2) {$ (("#" +s1+ "option:selected")). Appendto ($ ("#" +S2));
}

Later found in jquery, if the use of the function name plus parentheses, is executed, so I bind the event executed, such as SELECT () on the implementation, and then Baidu did not find the answer, went to Google a bit, obediently, and really found, Found the answer on the StackOverflow forum. And then my code becomes this:
Copy Code code as follows:

$ (function () {
var obj1 = {s: "Select1", S2: "Select2"};
var obj2 = {s: "Select2", S2: "Select1"};
$ ("#1"). Click (Obj1,select);
$ ("#2"). Click (OBJ1,SELECT2);
$ ("#3"). Click (Obj2,select);
$ ("#4"). Click (OBJ2,SELECT2);
function Select (event) {
Console.debug (EVENT.DATA.S);
$ ("#" +event.data.s+ "option:selected")). Appendto ($ ("#" +EVENT.DATA.S2));
}
function Select2 (event) {
$ ("#" +event.data.s+ "option"). Appendto ($ ("#" +EVENT.DATA.S2));
}
});

The data in Click (DATA,FN) is actually a JSON object, which can only be fetched by the current event source, and data is placed by default in event, so the data here is EventData, and event.data.name is used when quoting. In other words, all the trigger time methods in jquery require passing parameters to pass arguments through the EventData object:
Here to share the code of the Foreigner:
Copy Code code as follows:

$ ("Select#test"). Change ({msg: "OK"}, function (event) {
MyHandler (EVENT.DATA.MSG);
});

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.