admin管理员组文章数量:1245086
I ran into a bit of trouble while trying to load an external HTML page into a div using jQuery-ajax.
I had this div: <div id="content"></div>
and wanted to fill it with $("#content").load("include/add.html");
It loads the HTML file perfectly, but inside that add.html is a button that should load add2.html(also using .load), but it seems that neither the button nor the datepicker in that file work.
I'm guessing the .load function is responsible for that?
This is the content of add2.html:
<p>Nr: <input type="text"></input></p>
<p>Name: <input type="text"></input></p>
<p>Date: <input type="text" id="datepicker"></input></p>
<a href="#" id="button1">Next</a>
Please help, I'm desperate :D
I ran into a bit of trouble while trying to load an external HTML page into a div using jQuery-ajax.
I had this div: <div id="content"></div>
and wanted to fill it with $("#content").load("include/add.html");
It loads the HTML file perfectly, but inside that add.html is a button that should load add2.html(also using .load), but it seems that neither the button nor the datepicker in that file work.
I'm guessing the .load function is responsible for that?
This is the content of add2.html:
<p>Nr: <input type="text"></input></p>
<p>Name: <input type="text"></input></p>
<p>Date: <input type="text" id="datepicker"></input></p>
<a href="#" id="button1">Next</a>
Please help, I'm desperate :D
Share Improve this question asked Mar 29, 2010 at 12:50 ehehhhehehhh 1,0743 gold badges16 silver badges27 bronze badges1 Answer
Reset to default 12As the file is included to the page after the events are bound, the events do not apply to the elements in 'add.html'. You need to bind the events again or use .on()
method. In the case of the datepicker, it's easier to go with the first approach:
$("#content").load("include/add.html", function() {
// Apply datepicker to elements in 'add.html'
$('#content .date').datepicker();
});
For the button in add.html
, you can use delegation:
$(document).on('click', '#content .button_in_add', function() {
alert("I work even when included dynamically!");
$('#content').load('include/add2.html');
});
Note that .on()
bins does not go inside the .load()
callback as the datepicker part did. If you have any elements in 'add2.html', you need to repeat the same steps for that also.
本文标签: javascriptLoading HTML into ltdivgt with jQueryajaxStack Overflow
版权声明:本文标题:javascript - Loading HTML into <div> with jQuery-ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740244539a2247997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论