开发者

onclick call jQuery function

I have form with actively populated elements, something like:

<input id="id1" type="button" name="some_name">
<input id="id2" type="button" name="another_name">

etc.

My options are limited here.

I do not know id, as it is assigned by system dynamically (see: 1,2 next to id), so I cannot call JQ function by #. Order of elements change, they are shown in different sets etc.

Class manipulation is not practical. Same goes for any other attrib - pretty much - some are used, some may be used - again - dynamically, so I do not want to mess with it.

I can attach a js event thou (during form generation), e.g. onClick to particular, desired item.

<input id="id1" onClick="myFunc()" type="button" name="some_name">
<inp开发者_StackOverflow中文版ut id="id2" type="button" name="another_name">

I would not need to know id, class or anything in standard JS, since if element with myFunc is clicked, function is fired.

Is it possible to accomplish something similar with JQ?


Since you can attach your onlick event inline, why not add a class at that point instead?

Adding a class to your input will allow you to hook into it easily with jQuery.

<input class="myClass" [...] >

$('.myClass').click(function(){ [...] });

Update

To directly answer the question you seem to be asking, no, jQuery doesn't have a unique way of firing events inline by using the HTML binding attributes.

However, any function you create in Javascript can be fired inline. I do not recomend this approach since it's outdated and violates the seperation of concerns.


I fail to see why you wouldn't use attributes as a reference. You could do:

$("input[type=button][name=some_name]")

Or a generic handler:

$("input[type=button]").click(genericEventHandler);
function genericEventHandler()
{
  var clickedButton = $(this);
  /* ... */
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜