使用 .load() 加载页面片段不包括嵌入的 javascript

发布于 2024-10-07 20:27:50 字数 1574 浏览 0 评论 0原文

我有一个使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-10-14 20:27:50

.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?

情场扛把子 2024-10-14 20:27:50

是的,他只想加载页面片段。第一个参数确实是 url,因此只需输入“company_form”所在页面的名称,然后添加所需的片段即可。所以它应该看起来像这样......

$('#new-company-form').load('company_form.php #autoform');

我想唯一的变化是将扩展名添加到“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...

$('#new-company-form').load('company_form.php #autoform');

I guess the only change is add the extension to 'company_form'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文