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

May 16, 2016 pm 05:34 PM
change click Pass parameters

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);
});
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use v-on:click to monitor mouse click events in Vue How to use v-on:click to monitor mouse click events in Vue Jun 11, 2023 am 10:12 AM

Vue is a popular front-end framework that helps developers build websites and applications more conveniently and quickly. Among them, v-on:click is the instruction in Vue used to monitor mouse click events. Let's introduce how to use v-on:click in Vue to monitor mouse click events. First of all, using v-on:click in Vue can define mouse click events in two ways: directly in the template and in the Vue instance. Below we will introduce these two methods respectively. direct

Function parameter passing methods in Python *args, **kwargs, and others Function parameter passing methods in Python *args, **kwargs, and others Apr 13, 2023 am 09:58 AM

This article will discuss Python’s function parameters. We will understand what args and **kwargs, / and are. Although this problem is a basic python problem, we often encounter it when we write code. For example, timm uses this parameter passing method extensively. Defining and Passing Parameters What is the difference between parameters and arguments? Many people use these terms interchangeably, but there is a difference: Parameters are the names defined in the function definition Arguments are the values ​​passed to the function Red are parameters, green are are arguments. There are two ways to pass parameters: position and relationship.

Pass a variable number of arguments using the func_get_args() function Pass a variable number of arguments using the func_get_args() function Jun 27, 2023 pm 12:31 PM

In PHP, sometimes we need to write functions that can handle a variable number of arguments. The traditional approach is to use an array or pass each parameter individually. However, PHP also provides a simpler way, which is to use the func_get_args() function. The func_get_args() function can get all the parameters in the called function and store them in an array. This array can be used to handle any number of parameters. Let's demonstrate how to use func

What should I do if the 'click' event binding is invalid in my Vue application? What should I do if the 'click' event binding is invalid in my Vue application? Jun 24, 2023 pm 03:51 PM

Vue is a popular JavaScript framework for building modern web applications. In Vue, we usually use directives to operate DOM elements. Among them, the "click" event is one of the commonly used instructions. However, in Vue applications, we often encounter situations where the "click" event binding is invalid. This article explains how to solve this problem. The first step in checking whether an element exists is to confirm whether the element to which you want to bind a "click" event exists. If the element does not exist,

How to use v-on:click.self in Vue to trigger events only by yourself How to use v-on:click.self in Vue to trigger events only by yourself Jun 11, 2023 pm 01:57 PM

Vue is a popular front-end framework that is simple, efficient, and easy to maintain, and is loved by developers. In Vue, we often need to bind events to components or elements to achieve specific interactive effects, but sometimes we want the event to be triggered only by itself, without interference from other factors. So how to use v-on:click.self in Vue to trigger events only by yourself? This article will give you detailed answers. First, we need to understand the basic usage of the v-on instruction. The v-on directive is used to bind events, commonly used

Query the method of passing parameters from another JSP page (jQuery) Query the method of passing parameters from another JSP page (jQuery) Feb 27, 2024 am 10:45 AM

Title: How to use jQuery to pass parameters in JSP pages When developing web applications, you often encounter situations where you need to pass parameters from one JSP page to another JSP page. At this time, you can use jQuery to easily implement parameter passing. This article will introduce how to use jQuery to pass parameters in JSP pages and provide specific code examples. 1. Steps to use jQuery to pass parameters. Using jQuery to pass parameters in a JSP page generally requires the following steps:

How to use v-on:click.capture to implement event processing in the capture phase in Vue How to use v-on:click.capture to implement event processing in the capture phase in Vue Jun 11, 2023 am 10:55 AM

Vue is a popular JavaScript framework that provides developers with a variety of instructions and methods, allowing developers to deal with various problems encountered in web development more efficiently. Among them, the v-on directive can be used to bind handlers for various events, and v-on:click.capture means that the capture phase is used when processing click events. In JavaScript, the event propagation process is divided into three stages: capture stage, target stage and bubbling stage. At the capture stage

How to use v-on:click.stop to stop event bubbling in Vue How to use v-on:click.stop to stop event bubbling in Vue Jun 11, 2023 pm 12:00 PM

Vue is a very popular JavaScript framework in the front-end community. It can help us build efficient, flexible and easy-to-maintain web applications. Event bubbling is a very common problem in Vue, because in complex applications, multiple components may share the same DOM element. In this case, it is very convenient to use the v-on:click.stop directive to stop event bubbling. 1. What is event bubbling? Event bubbling refers to when an event on a DOM element is triggered

See all articles