如何在.click()函数中传递参数? [重复]

时间:2022-09-04 21:06:24

This question already has an answer here:

这个问题在这里已有答案:

I want to pass arguments to the click() handler function in the following case:

我希望在以下情况下将参数传递给click()处理函数:

function edit_tab(val){
    var prev_tab = $("#current_tab").html();
    if (prev_tab == 1) {
        $('.edit-basic-info').click(a); // here I want to pass some argument "a".
    }
}

How can I pass the argument a in the above code? The way I am trying to do is not working.

如何在上面的代码中传递参数a?我试图做的方式不起作用。

4 个解决方案

#1


6  

You can use trigger()'s extraParameters to pass additional parameters to your handler:

您可以使用trigger()的extraParameters将其他参数传递给您的处理程序:

From the trigger() documentation:

来自trigger()文档:

extraParameters Type: Array or PlainObject Additional parameters to pass along to the event handler.

extraParameters类型:Array或PlainObject要传递给事件处理程序的其他参数。

Example (also from the documentation):

示例(也来自文档):

$( "#foo" ).on( "custom", function( event, param1, param2 ) {
  alert( param1 + "\n" + param2 );
});

$( "#foo").trigger( "custom", [ "Custom", "Event" ] );

#2


2  

Use a closure:

使用闭包:

function doSomething(myParam) {
    return function(event) {
        // Use both myParam and event here
    };
}

And in your event handler:

并在您的事件处理程序中:

$('#foo').on('click', doSomething('parameter')); // Works fine

The reason this works is that the call to doSomething('parameter') returns a function, the inner function is not executed until the click handler calls it, and it can use all of the parameters passed to it from the outside.

这样做的原因是对doSomething('parameter')的调用返回一个函数,内部函数在click处理程序调用之前不会执行,并且它可以使用从外部传递给它的所有参数。

#3


-1  

bit more you can do like this

你可以这样做更多

var message = 'check'; 
$('button').click({msg: message},functionFoo);

function functionFoo(e){
 alert(e.data.msg);
}

#4


-1  

    <script>
    function edit_tab(val){
                var prev_tab=$("#current_tab").html();
                if(prev_tab==1){
                    $('.edit-basic-info').bind('click', function () {  
                       fndosomething(a)
//pass value in that function and use it

     });
                }
    }
    </script>

#1


6  

You can use trigger()'s extraParameters to pass additional parameters to your handler:

您可以使用trigger()的extraParameters将其他参数传递给您的处理程序:

From the trigger() documentation:

来自trigger()文档:

extraParameters Type: Array or PlainObject Additional parameters to pass along to the event handler.

extraParameters类型:Array或PlainObject要传递给事件处理程序的其他参数。

Example (also from the documentation):

示例(也来自文档):

$( "#foo" ).on( "custom", function( event, param1, param2 ) {
  alert( param1 + "\n" + param2 );
});

$( "#foo").trigger( "custom", [ "Custom", "Event" ] );

#2


2  

Use a closure:

使用闭包:

function doSomething(myParam) {
    return function(event) {
        // Use both myParam and event here
    };
}

And in your event handler:

并在您的事件处理程序中:

$('#foo').on('click', doSomething('parameter')); // Works fine

The reason this works is that the call to doSomething('parameter') returns a function, the inner function is not executed until the click handler calls it, and it can use all of the parameters passed to it from the outside.

这样做的原因是对doSomething('parameter')的调用返回一个函数,内部函数在click处理程序调用之前不会执行,并且它可以使用从外部传递给它的所有参数。

#3


-1  

bit more you can do like this

你可以这样做更多

var message = 'check'; 
$('button').click({msg: message},functionFoo);

function functionFoo(e){
 alert(e.data.msg);
}

#4


-1  

    <script>
    function edit_tab(val){
                var prev_tab=$("#current_tab").html();
                if(prev_tab==1){
                    $('.edit-basic-info').bind('click', function () {  
                       fndosomething(a)
//pass value in that function and use it

     });
                }
    }
    </script>