How to send multiple arguments to jQuery click fun

2019-02-09 00:28发布

问题:

Currently I'm using something like:

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

And 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

回答1:

jQuery Events

jQuery .click()

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


回答2:

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



回答3:

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.



回答4:

Quick Solution

<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.