简体   繁体   English

经典的jQuery错误-Uncaught TypeError:undefined不是一个函数

[英]A classical jQuery error - Uncaught TypeError: undefined is not a function

I have some JS functions in my page, and I got this error. 我的页面中有一些JS函数,但出现此错误。 I removed everything but the incriminated lines and I still get this error. 我删除了所有的行,但不包括内嵌的行,仍然出现此错误。

I searched this forum and the web, so I tried to add : semicolons, (jquery), add the $ to the function arguments...I still get the same error. 我搜索了这个论坛和网络,因此尝试添加:分号(jquery),将$添加到函数参数中...我仍然遇到相同的错误。

I use jQuery 1.11.2, declared in my header. 我使用在标头中声明的jQuery 1.11.2。

Here is the whole code for those who asked : 这是那些询问的人的完整代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>  
<script type= "text/javascript" src= "jquery-1.11.2.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>

<h2>Exporter les données d'une base<BR><BR></h2>
<HR></HR>
<form method="post" action="execute_function.php" enctype="multipart/form-data">
<?php
include 'functions.php';
propose_choix_BDD();
?>
<BR>
<div class="camp">
</div>
<div class="fichier">
</div>
<BR><BR><BR>

<div class="button">
<?php
submit_button("Export_KML");
submit_button("Export_csv");
?>  
</div>
<input type= "hidden" name="export_campagne" value="true" />
</form>


    <script type="text/javascript">
    console.log("I start");
    $(function(){
        $('.camp').on('change',"#id_camp_ch",function()  { //error here
            alert ("I change !");
            }); 
    console.log("I stop");
    }); 

    </script>
</body>
<?php
retour_index();
?>

propose_choix_BDD() echoes a dropdown with DB inside, div class="camp" contains some tables echoed when a jQuery change is detected in the DB menu (script removed for clarity reasons). proposal_choix_BDD()回显一个包含DB的下拉列表,div class =“ camp”包含一些在DB菜单中检测到jQuery更改时回显的表(出于清晰原因删除了脚本)。 So .camp is dynamically filled. 所以.camp是动态填充的。 Once it is, I'd like my JS to detect a change made by the user in the dropdown menu. 一旦完成,我希望我的JS在下拉菜单中检测用户所做的更改。 #id_camp_ch is the name of the id of the that wraps this menu. #id_camp_ch是包装此菜单的ID的名称。

I agree that as I have cut a part of my code, .camp will remain empty. 我同意在削减部分代码后,.camp将保持空白。 In other words, #id_campagne_choix will never exist. 换句话说,#id_campagne_choix将永远不存在。 But it won't exist in the beginning too even if I use my full script. 但是即使我使用完整的脚本,它也不会在一开始就存在。 So I shouldn't get such an error ... 所以我不应该得到这样的错误...

I also tried to put all my script at the bottom of the document. 我还尝试将所有脚本放在文档的底部。

If I remove the lines $( ".camp" )...}) the alert does display. 如果删除$(“ .camp”)...})行,则会显示警报。

If you have any idea for this it'll be awesome :) thanks 如果您对此有任何想法,那就太好了:)谢谢

Their is a error, Please try the following code... 他们是错误的,请尝试以下代码...

onload = function() {

            $( ".camp" ).on("change","#id_camp_ch",function()  {
                alert ("change !");
                });             
        };

But rather you should use 但是你应该使用

$(document).ready(function(){
$( ".camp" ).on("change","#id_camp_ch",function()  {
                    alert ("change !");
                    }); 
});

The onchange will fire when DOM is ready. DOM准备就绪时,将触发onchange

I didn't understanded the pourpose of changing a div, cause divs dont have the change listener. 我不了解更改div的目的,因为div没有更改侦听器。

U could use one of this examples to help: U可以使用以下示例之一来帮助您:

<form method="post" action="execute_function.php" enctype="multipart/form-data">
<BR>
change me!
<input type="text" class="camp"><br>
or me
<input type="text" id="id_camp_ch"><br>
dont change me! i won't make anything!
<input type="text" id="id_camp_ch" class="camp">
</id>
<div class="fichier">
And never change ME!!!!!
<select id="test">
<option></option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</div>
<BR><BR><BR>

<div class="button">
</div>
<input type= "hidden" name="export_campagne" value="true" />
</form>

<script type="text/javascript">

$('#id_camp_ch.camp').change(function()  {
alert ("I said to u dont change me!!!"); //or do what u need
});
</script>

I think also that the div tag dont have the change listener, that's why it's throwing the error 我还认为div标签没有更改侦听器,这就是它引发错误的原因

if the id_camp_ch is a children of the div with style camp, or someelse than u should use .camp>#id_camp_ch instead of .camp#id_camp_ch 如果id_camp_ch是具有样式营地的div的子代,或者比您更应该使用.camp> #id_camp_ch而不是.camp#id_camp_ch

think u can solve it by using the right selectors instead of using the .on() function 认为您可以通过使用正确的选择器而不是使用.on()函数来解决此问题

I finally downloaded once again jQuery v2.1.1. 我终于再次下载了jQuery v2.1.1。 and it worked ! 而且有效! Thanks for your help 谢谢你的帮助

Include jquery before using it. 在使用jquery之前先添加它。

Enjoy :) 请享用 :)

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

相关问题 jQuery的,遗漏的类型错误:未定义是不是一个函数 - Jquery, Uncaught TypeError: undefined is not a function 未捕获的TypeError:undefined不是jQuery的函数 - Uncaught TypeError: undefined is not a function with jQuery 未捕获的TypeError:undefined不是函数-不是JQuery - Uncaught TypeError: undefined is not a function - not JQuery 未捕获的TypeError:undefined不是函数jquery - Uncaught TypeError: undefined is not a function jquery 错误:未捕获的类型错误:未定义不是函数 - Error: Uncaught TypeError: undefined is not a function Javascript JQuery Listview刷新错误消息-未捕获的TypeError:undefined不是函数 - Javascript JQuery Listview Refresh Error Msg - Uncaught TypeError: undefined is not a function JQuery选择菜单返回错误Uncaught TypeError:undefined不是函数 - JQuery select menu returning error Uncaught TypeError: undefined is not a function 未捕获的TypeError:undefined不是函数(jQuery和jQuery Easing) - Uncaught TypeError: undefined is not a function (jQuery & jQuery Easing) jQuery 错误:未捕获的类型错误:$ 不是函数 - jQuery Error: Uncaught TypeError: $ is not a function 未捕获的TypeError:未定义不是我的jQuery脚本中的函数 - Uncaught TypeError: undefined is not a function in my jQuery script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM