简体   繁体   English

jQuery脚本在加载的jQuery页面中不起作用

[英]Jquery script doesn't work in jquery loaded page

I have tried to search for similar questions, but I could not find anything, so if you know any similar question please let me know. 我曾尝试搜索类似的问题,但找不到任何东西,因此,如果您知道任何类似的问题,请告诉我。

Let me explain what Im doing: I have a script that validates forms in a js file, It works fine in any page with a form I have, the problem is that when I load a form using jquery it just doesn't work, I have tried using the next line in different places: Header, footer, etc 让我解释一下我在做什么:我有一个脚本来验证js文件中的表单,在拥有表单的任何页面中都可以正常工作,问题是当我使用jquery加载表单时,它根本无法正常工作,我尝试在不同位置使用下一行:页眉,页脚等

<script src='myFile.js'></script>

By far the only thing that has worked for is writing the line of code above inside the form itself. 到目前为止,唯一有效的方法是在表单本身内部编写上面的代码行。 I think it has something to do with the form that the DOM works, I have also tried using and not using it. 我认为这与DOM的工作形式有关,我也尝试使用和不使用它。

$(document).ready(function (){ //code});

It only will work when I add the script tag with the src attribute inside the form itself. 仅当我在表单本身内部添加具有src属性的脚本标签时,它才起作用。

It would not represent a big problem for me to add the script tag to any form I load using jquery but it's a little bit more of work an unefficient, and also when I add the script tag to any form and load it using ajax I get the next console warning that only goes away when I remove the script tag from the form file: 对于我来说,将jquery标签添加到我使用jquery加载的任何表单中都不会是一个大问题,但这会增加工作效率,并且当我将script标签添加到任何表单并使用ajax加载时我也得到了下一个控制台警告仅在我从表单文件中删除脚本标记时消失:

jquery-3.2.1.min.js:4 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. jquery-3.2.1.min.js:4 [Deprecation]不赞成在主线程上使用同步XMLHttpRequest,因为它对最终用户的体验有不利影响。 For more help, check https://xhr.spec.whatwg.org/ . 如需更多帮助,请访问https://xhr.spec.whatwg.org/

Here is a part of my code: 这是我的代码的一部分:

<!--home.html-->
<div id='formFrame'>
</div>

<script>

    $("#formFrame").load("html/loginForm.html");

</script>
<!--end of home.html-->

<!--loginForm.html-->
<form action='somePage.php' method='post' id='loginForm'>
    <input type='email' name='email' placeholder='email'>
    <input type='password' name='password' placeholder='password'>
    <input type='submit' name='submit' value='Login'>
</form>
<script src='js/validate.js'></script>
<!--end of loginForm.html-->

<!--validation script (validate.js)-->
$(document).ready(function (){
    $("#loginForm").submit(function (e){
        e.preventDefault();
        alert("Working");
    });
});

Thanks for spending some of your valuable time on reading this, I appreciate it a lot! 感谢您花费宝贵的时间阅读本文,非常感谢!

As I can't comment, putting my comment in answer! 由于我无法发表评论,因此请将我的评论作为答案!

I am not sure what you have written in validate.js, but if you are using jQuery unobtrusive validation, then you must rebind the validators to the form if you are loading it dynamically. 我不确定您在validate.js中编写的内容,但是如果您使用的是jQuery非侵入式验证,那么如果要动态加载表单,则必须将验证器重新绑定到表单。

I was facing same issue in ASP.NET MVC while loading forms using AJAX. 使用AJAX加载表单时,我在ASP.NET MVC中面临着相同的问题。 I am not sure will it help you or not but below is the code. 我不确定它是否对您有帮助,但是下面是代码。

 $("#formFrame").load("html/loginForm.html", function(){
    var $form = $("formSelector");
    $form.removeData('validator');
    $form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse($form);
});

Well guys I found a solution: 好吧,我找到了解决方案:

As there seems to be a conflict when loading an external page using ajax (.load) I opted to use php instead of javascript which worked fine: 由于使用ajax(.load)加载外部页面时似乎存在冲突,因此我选择使用php而不是javascript,效果很好:

I removed the next code 我删除了下一个代码

<script>

    $("#formFrame").load("html/loginForm.html");

</script>

And added the next to the div where I want to load my content: 并将下一个添加到要加载内容的div中:

<!--home.php (changed html to php)-->
<div id='formFrame'>
    <?php
        require 'html/loginForm.html';
    ?>
</div>

I would have prefered to use the load method from ajax to avoid loading the content befere the user could request it, but by far that's the only solution I have been able to think about. 我本来希望使用ajax中的load方法来避免在用户可以请求时加载内容,但是到目前为止,这是我唯一能想到的解决方案。 Thanks to everybody for your help it was really helpful! 感谢大家的帮助,它真的很有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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