javascript or jQuery doesn't work in dynamically generated content

半世苍凉 提交于 2019-11-28 00:24:08

For dynamically generated elements you should delegate the events, you can use the on method:

$(function() {
    $(document).on('click', '#submitUser', function(e) {
       var fname = $("#fname").val();
       $("#theresult").text(fname);
       e.preventDefault();
    });
});

live() method is deprecated.

Taking a wild stab in the dark here since your question isn't very well-described, but perhaps you're trying to use .click() and so on to bind events to things that are getting dynamically loaded into the page?

If so, you probably want to look at .on() instead.

You need to use live event.

As an example, if your generated content contain a click event, you could do this:

$(".something").live("click", function({ 
// do something
)};

For dynamically generated fields use .on() JQuery method:

$(document).on('click', '#MyID', function(e) {
    e.preventDefault(); // or you can use "return false;"
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!