Javascript 未定义 onChange 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12621258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:51:32  来源:igfitidea点击:

onChange function is not defined

javascriptjqueryundefinedonchange

提问by Expedito

This has got to be something simple. I searched the internet and only found syntax errors as the cause of this problem, but I can't find a syntax error.

这必须是一些简单的事情。我在网上搜索,只发现语法错误是这个问题的原因,但我找不到语法错误。

Here's the javascript :

这是 javascript :

<script type="text/javascript" src="http://localhost/acrilart/javascript/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        function hi(){
            alert('hi');
        }
        hi();
    });
</script>

And the HTML :

和 HTML :

<input type="text" name="cep" value="" id="cep" class="required cep field"
       onChange="hi()" />

On pageload the function hiis called as expected, but my onChangeevent causes a Firebug error, saying the function is not defined. I'm really stumped. Did I mispell 'hi'?

在页面加载时,函数hi按预期调用,但我的onChange事件导致 Firebug 错误,说该函数未定义。我真的很难过。我拼错了'嗨'吗?

回答by James Allardice

The hifunction is only in scope inside the readyevent handler. Move it outside of the event handler, or handle the binding inside there (and remove the inline event handler attribute from the markup):

hi函数仅在ready事件处理程序内的范围内。将它移到事件处理程序之外,或在那里处理绑定(并从标记中删除内联事件处理程序属性):

$(document).ready(function(){
    function hi(){
        alert('hi');
    }
    $("#cep").on("change", hi);
});

回答by Niet the Dark Absol

The hifunction is only defined in the readyblock. Outside, it doesn't exist anymore.

hi功能仅在ready块中定义。在外面,它已经不存在了。

You don't need to wrap function definitions in .ready(), so just remove it. Alternatively, define the function like this:

您不需要将函数定义包装在 中.ready(),因此只需将其删除即可。或者,像这样定义函数:

window.hi = function() {...}

回答by LeGEC

In your code block:

在您的代码块中:

<script type="text/javascript">
    $(document).ready(function(){
        function hi(){
            alert('hi');
        }
        hi();
    });
</script>

hiis not a global function. You can access it only inside the scope of your function(){...}, not from outside.

hi不是全局函数。您只能在您的范围内访问它function(){...},而不能从外部访问它。

Since you are using jQuery, you can change the way you bind your function to the onChangeevent. Rather than calling it from the html tag, you can write:

由于您使用的是 jQuery,您可以更改将函数绑定到onChange事件的方式。而不是从 html 标签调用它,你可以写:

<script type="text/javascript">
    $(document).ready(function(){
        function hi(){
            alert('hi');
        }
        hi();

        $('#cep').on( 'change', function(){ hi(); } );
    });
</script>

回答by Sender

onchangeis only triggered when the control is blurred. Try onkeypressinstead.

onchange仅在控件模糊时触发。试试吧onkeypress

$("#cep").on("change", function() {
   alert(1);
});

or

或者

<input type="text" name="cep" value="" id="cep" class="required cep field" 
onkeypress="hi()"  />

Use following events instead of onchange:

使用以下事件代替onchange

- onkeyup(event)
- onkeydown(event)
- onkeypress(event)

回答by Amrish Prajapati

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript"> 
function hi(){ 
    alert('hi'); 
} 
</script>

<input type="text" name="cep" value="" id="cep" class="required cep field" onKeyPress="javascript:hi();" />