简体   繁体   中英

jquery script not working when loading with .load a remote html

I have this code

$(document).ready(function() {
    $(function () {
        $('#id_type').on('change', function () {
            var k = $(this).val();
            if (k == "ci") {
                $(".input_id").attr("placeholder", "<?php echo $lg["ci_ej"]; ?>").placeholder();
            }
            else if (k == "rif") {
                $(".input_id").attr("placeholder", "<?php echo $lg["rif_ej"]; ?>").placeholder();
            }
            else if (k == "pas") {
                $(".input_id").attr("placeholder", "<?php echo $lg["passport_ej"]; ?>").placeholder();
            }
        });
        $('input[placeholder], textarea[placeholder]').placeholder();
    });
});

it works pretty well when I go directly to the page that use it, but When I call an specific div from the remote html using .load like this:

$('.loadiv').load($(this).attr("href") + " .windows-wrapper");

the call works well but the first code stop working, i don't understand why. thanks in advance for helping me.

如果您尝试从远程js文件执行此操作,则无法将php代码执行到.js文件中。

If you loaded some html and want JQuery to watch for them, the live method is your friend. Or just rebind JQuery to your document.

I happens because when the page is loaded, JQuery is loaded right after, knowing what the document has.

When you dynamically add new elements, JQuery doesn't know about them because they were loaded after JQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM