使用 .load() 加载页面片段不包括嵌入的 javascript
我有一个使用 .load() 方法加载的表单。
$(function() {
$('#items,#button-search').hide();
$("#companies").click(function() {
$(this).attr("value","");
$('#address,#new-company-form').empty();
});
$( "#companies" ).autocomplete({
source: ";companies",
minLength: 2,
select: function( event, ui ) {
if(ui.item.id == "create-new-company") {
// call the new company form
$('#address').empty();
$('#new-company-form').load(';company_form #autoform');
}
else
{
$('#new-company-form').empty();
$.ajax({
type: 'GET',
url: ';addresses?company=' + ui.item.id,
dataType: 'json',
// process the addresses
success: function(json) {
var opts = '';
$.each(json, function(k, v) {
opts += '<option>' + v + '</option>';
});
$('#address').html('<select>' + opts + '</select>');
}
}); //end ajax call to address
}
} // end select address
}); // end autocomplete
}); // end function
在第 15 行,
$('#new-company-form').load(';company_form #autoform');
如果我删除 #autoform,则 ;company_form html 中的 javascript 会起作用,否则不会加载。
我该如何让它发挥作用?
谢谢
I have a form that is loaded using the .load() method.
$(function() {
$('#items,#button-search').hide();
$("#companies").click(function() {
$(this).attr("value","");
$('#address,#new-company-form').empty();
});
$( "#companies" ).autocomplete({
source: ";companies",
minLength: 2,
select: function( event, ui ) {
if(ui.item.id == "create-new-company") {
// call the new company form
$('#address').empty();
$('#new-company-form').load(';company_form #autoform');
}
else
{
$('#new-company-form').empty();
$.ajax({
type: 'GET',
url: ';addresses?company=' + ui.item.id,
dataType: 'json',
// process the addresses
success: function(json) {
var opts = '';
$.each(json, function(k, v) {
opts += '<option>' + v + '</option>';
});
$('#address').html('<select>' + opts + '</select>');
}
}); //end ajax call to address
}
} // end select address
}); // end autocomplete
}); // end function
on line 15, i have
$('#new-company-form').load(';company_form #autoform');
if i remove the #autoform the javascript that is in the ;company_form html works, otherwise it is not loaded.
how do i get this to work?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.load()
方法的第一个参数是 url。您在这里需要的 url 可能只是;company_form
。您想通过在 url 中传递#autoform
来实现什么目的?如果它在没有
#autoform
的情况下也能工作,那么为什么不直接删除它呢?The first parameter of the
.load()
method is the url. The url that you need here is probably just;company_form
. What do you want to achieve by passing#autoform
in the url?If it works without the
#autoform
, then why don't you simply remove it?是的,他只想加载页面片段。第一个参数确实是 url,因此只需输入“company_form”所在页面的名称,然后添加所需的片段即可。所以它应该看起来像这样......
我想唯一的变化是将扩展名添加到“company_form”
Yeah he wants to load just the page fragment. The first argument is indeed url, so just put the name of the page that 'company_form' resides in then add the fragment you want. So it should look something like this...
I guess the only change is add the extension to 'company_form'