如何向 jQuery 单击函数发送多个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1541127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:41:05  来源:igfitidea点击:

How to send multiple arguments to jQuery click function?

jqueryclickparameter-passing

提问by Roger

Currently I'm using something like:

目前我正在使用类似的东西:

$('.myclass').click(function(){  
    var msg = $(this).attr('id');  
    alert(msg)
});

And HTML:

和 HTML:

< a href="#" class="myclass" id="101">Link</a>

If I need additional parameters how would I read them? Also is the current way I am using the proper way? Originally I was using hidden input fields so it was already a step up. :p

如果我需要额外的参数,我将如何读取它们?我目前使用的方式是否正确?最初我使用的是隐藏的输入字段,所以这已经是一个进步。:p

回答by ChaosPandion

jQuery Events

jQuery 事件

jQuery .click()

jQuery.click()

$('.myclass').bind("click", { Param1: "", Param2: 2 }, function(event){
    alert(event.data.Param2);
});

回答by Russ Cam

you can use bind()and send additional data to the event handler as event.data

您可以使用bind()并将其他数据发送到事件处理程序event.data

回答by Jason

this may not be the best method, but something i have done is to hyphen delineate the id attribute with other params, like id="101-red-420582"which all three mean different things and can be easily broken down using the split()function. not the best, per se, but i've done it and it works.

这可能不是最好的方法,但我所做的事情是用连字符用其他参数描述 id 属性,就像id="101-red-420582"这三个参数代表不同的东西,可以使用split()函数轻松分解。本身不是最好的,但我已经做到了,而且效果很好。

回答by Anshu

Quick Solution

快速解决方案

<p class="msg_head" id="837" rel="a" **till_limit="9"**>Header-1 </p>

<p class="msg_head" id="837" rel="a" **till_limit="9"**>Header-1 </p>

and you can refer as

你可以参考

 $(this).attr('till_limit');

This is similar solution like define your own ID and refer the element as per your requirement.

这是类似的解决方案,例如定义您自己的 ID 并根据您的要求引用元素。