简体   繁体   English

Firebug控制台,显示错误“功能未定义”

[英]Firebug console, giving error “function not defined”

I am trying to submit a form through Ajax, but it is not calling the function? 我正在尝试通过Ajax提交表单,但是没有调用该函数?

I'm getting this error on the Firebug console: 我在Firebug控制台上收到此错误:

ReferenceError: CreateUser is not defined
CreateUser()

The button I am using to call this function: 我用来调用此功能的按钮:

<input type="button" onclick="CreateUser()" id="RegisterUserBtn" class="btn" value="Sign Up" name="">

My function: 我的功能:

<script language="JavaScript">
    $(document).ready(function(e){

        function  CreateUser(){
            alert('CreateUser SignUp button is clicked.');
        }
    });

</script>

What is causing this issue? 是什么导致此问题? Why cant it find the function name? 为什么找不到函数名?

You are mixing two styles of javascript-coding here: "old style" with on-event attributes in your html code. 您将在此处混合使用两种javascript编码样式:“旧样式”和html代码中的事件属性。 and "unobstrusive javascript" with the $(document).ready block. 和带有$(document).ready块的“ unobstrusive javascript”。 My recommendation would be to use unobstrusive javascript consistently: 我的建议是始终使用不引人注目的JavaScript:

Don't use onclick in your html. 不要在HTML中使用onclick。 jQuery helps you keep all Javascript out of your html code. jQuery帮助您将所有Javascript排除在html代码之外。 You already have an ID for you button. 您已经有一个ID用于您的按钮。

<input type="button"  id="RegisterUserBtn" class="btn" value="Sign Up" name="">

so your jQuery could look like this: 因此您的jQuery可能如下所示:

$(document).ready(function(e) {
    function CreateUser() {
        alert('CreateUser SignUp button is clicked.');
    }
    $('#RegisterUserBtn').on('click', CreateUser);
});

This separation of HTML and Javascript is called "unobstrusive javascript". HTML和Javascript的这种分离称为“不干扰JavaScript”。

See a live Demo here: http://jsfiddle.net/bjelline/nySxm/ 在此处观看现场演示: http : //jsfiddle.net/bjelline/nySxm/

You're creating a closure around your CreateUser function by putting it inside of an anonymous function. 您正在通过将CreateUser函数放在匿名函数内部来创建一个闭包。 CreateUser is "local" to the scope of the containing function and is not exposed to the global scope where your onclick event is being defined. CreateUser在包含函数的作用域内“本地”,并且未暴露于定义onclick事件的全局作用域。

Simply take the function outside of the $(document).ready block. 只需将函数$(document).ready块之外。

If you are using jquery then try this instead 如果您使用的是jQuery,请尝试使用此方法

<script language="JavaScript">
    $(document).ready(function(e){

       $("#RegisterUserBtn").click(function(){
            alert('CreateUser SignUp button is clicked.');
        });
    });
</script>

Hope this helps 希望这可以帮助

if your want to run CreateUser function on click event, then remove $(document).ready function and declare your function inside title. 如果要在click事件上运行CreateUser函数,则删除$(document).ready函数,并在标题中声明您的函数。 The functions inside $(document).ready load after the content load. $(document).ready内部的函数在内容加载后立即加载。 your html is loaded before the function declaration . 您的html在函数声明之前加载。

Or you may use jquery on function. 或者您可以函数使用jquery。

$(document).ready(function(e) {

   $('#RegisterUserBtn').on('click', function() {
       alert('CreateUser SignUp button is clicked.');
   });

});

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

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