admin管理员组

文章数量:1356368

I'm working with an existing system set up by someone else (no longer here). In this system, clicking on the text within a special <span> will trigger a js function which will replace the text with an <input> field. The text which was there is assigned as the value of the <input> element.

An onblur event is assigned to this new <input> field. This calls a function which updates the data in the database via an AJAX call. As part of this action, the <input> field is replaced with the new value (same <span> contents), and the onclick event is re-assigned. In this way, you can click on the text, change it, click elsewhere, and it is automatically updated in the database. Then you can do it again as it sets up the original events dynamically with each update.

It is in-between the first event and the second that I want access to that <input> field. I want to add a jquery .datepicker() to it.

How would I go about calling .datepicker() on a dynamically-created element?

I'm working with an existing system set up by someone else (no longer here). In this system, clicking on the text within a special <span> will trigger a js function which will replace the text with an <input> field. The text which was there is assigned as the value of the <input> element.

An onblur event is assigned to this new <input> field. This calls a function which updates the data in the database via an AJAX call. As part of this action, the <input> field is replaced with the new value (same <span> contents), and the onclick event is re-assigned. In this way, you can click on the text, change it, click elsewhere, and it is automatically updated in the database. Then you can do it again as it sets up the original events dynamically with each update.

It is in-between the first event and the second that I want access to that <input> field. I want to add a jquery .datepicker() to it.

How would I go about calling .datepicker() on a dynamically-created element?

Share Improve this question edited Dec 3, 2022 at 10:22 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 8, 2011 at 3:02 user965641user965641 1371 gold badge2 silver badges11 bronze badges 3
  • 2 Couldn't you just update the "js function which will replace the text with an <input>" to bind the datepicker as well? – mu is too short Commented Nov 8, 2011 at 3:07
  • actual code would help also... – jondavidjohn Commented Nov 8, 2011 at 3:08
  • @mu: it's being used all over, in many scripts, for all text input fields. but I only want to use it for one, so I don't want a calendar button after all those other input fields. makes things a bit more plicated. good idea, though. – user965641 Commented Nov 8, 2011 at 18:46
Add a ment  | 

1 Answer 1

Reset to default 4

No, there isn't an event similar to an onCreate. The closest you can find is jQuery's .live(). This allows you to bind an event to an element now or in the future. I also think it will probably solve your problem.

http://api.jquery./live/

$('input').live('click', function() {
   $(this).datepicker();
});

As AutoSponge handily pointed out, live is deprecated as of jQuery 1.7. They suggest using on or delegate instead.

http://api.jquery./on/

http://api.jquery./delegate/

本文标签: jqueryIn JavaScriptis there such an event as onCreateStack Overflow