English 中文(简体)
inding event event hand
原标题:Binding a jQuery event handler to multiple similar forms

I have a html page with multiple similar elements which can be edited in-place:

The Title
Some description
Count 10
[Edit]

Another Title
Another description
Count 54
[Edit]

...

如果ed子被点击,则案文用Ajax的表格取而代之(该网页的其余部分被重载):

Title:       [The Title       ]
Description: [Some description]
Count:       [10]
[Save] [Cancel]

Another Title
Another description
Count 54
[Edit]

...

The event handlers to replace the text with a form and to submit the form are almost identical for all items. My first approach was to use IDs in the html tags and bind the jQuery event handler to this ID. But hat leads to repetitive code. So the obvious idea is to not repeat similar code for each item.

我怎么能够区分手里的物品,以便用表格取代正确的项目,并提交正确的表格?

最佳回答
$( .classYouPutOnAllEditButtons ).click(function () {
    var that = $(this); // this variable now holds a reference to the Edit button that was clicked, you can use a traversal method (e.g. closest()) to find the form it was in
    // do your other stuff
});
问题回答

在您的活动手稿中,this系指被点击的具体内容。

$(yourSelector).bind( event , function () {
    // in here, `this` will be the Edit button.
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签