Home > Web Front-end > JS Tutorial > How to pass parameters in JQuery such as click(), change(), etc. Specific implementation_jquery

How to pass parameters in JQuery such as click(), change(), etc. Specific implementation_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 17:34:59
Original
1061 people have browsed it

Because we have to do such a job, which is to convert options in two selections. The picture is as follows:
How to pass parameters in JQuery such as click(), change(), etc. Specific implementation_jquery
This job is to add click() events to several buttons. The general usage is like this:

Copy code The code is as follows:

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

Then I looked up the official documentation and found out about click The description is like this, but later I still didn't get the answer from Baidu.
Considering the reusability of the code, I wanted to directly pass the "select1" and "select2" strings into it, so I used the following method:
Copy code The code is as follows:

$("#but_one").click(select("select1"," select2"));
//Improve code reusability, change according to function
function select(s1,s2){ $(("#" s1 "option:selected")).appendTo($("# " s2));
}

Later I discovered that in jQuery, if you use the function name with parentheses, it will be executed, so it will be executed when I bind the event, such as select(). Then I couldn’t find the answer on Baidu, so I went to Google and found it. I found the answer on the stackoverflow forum. Then my code became like this:
Copy code The code is 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 retrieved through the current event source. , data is placed in the event by default, so the data here is eventdata, and event.data.name is also used when referencing it. That is to say, all methods that trigger time in JQuery and need to pass parameters can be passed through the eventdata object. Parameters:
Share the foreigner’s code here:
Copy the code The code is as follows:

$("select#test").change({msg: "ok"}, function(event) {
myHandler(event.data.msg);
});
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template