onChange函数未定义 [英] onChange function is not defined

查看:952
本文介绍了onChange函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这必须简单。我搜索了互联网,发现语法错误只是造成这个问题的原因,但我找不到语法错误。

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.

这是javascript:

Here's the 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>

HTML:

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

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

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

推荐答案

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

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

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

这篇关于onChange函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆