Javascript Uncaught引用错误函数未定义 [英] Javascript Uncaught Reference error Function is not defined

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

问题描述



当我在文本框中添加数据(即使我将其保留为空)并尝试点击添加它不会做任何事情。



打开Chrome和Firefox控制台都给我同样的错误,它说changeText2()没有定义。



我该如何解决这个问题?我已经多次遇到这个错误,其中大部分都有非常奇怪的解决方法,但我不确定避免它的方法或者我做错了什么。



看起来,删除全局变量声明可以在大多数情况下修复它,但是,在这种情况下,我需要它们,并且想知道为何以及如何发生此错误。



任何和所有的帮助,非常感谢。



Javascript:
$ b

  var list = document.getElementById('deliveryIdArray'); 
var names = [];

function changeText2(){
var deliveryIdentification = document.getElementById('deliveryIdentification')。value;
names.push(deliveryIdentification); //只需将新名称添加到数组;
//数组已更改重新呈现列表
renderList();


函数renderList(){
while(list.firstChild){
list.removeChild(list.firstChild);
}
//再次创建每个li
for(var i = 0; i< names.length; i ++){
var entry = document.createElement('li') ;
entry.appendChild(document.createTextNode(names [i]));
var removeButton = document.createElement('button');
removeButton.appendChild(document.createTextNode(remove));
removeButton.setAttribute('onClick','removeName('+ i +')');
entry.appendChild(removeButton);
list.appendChild(entry);
}
}


函数removeName(nameindex){
names.splice(nameindex,1);
//数组已更改重新呈现列表
renderList();
}

函数getDeliveries(){
返回名称;

HTML:

 < b>编号:< / b> 
< input id =deliveryIdentificationname =deliveryIdentificationtype =textsize =16maxlength =30>

<! - 数组区域创建 - >
< input type ='button'onclick ='changeText2()'value ='Add'/>

< ol id =deliveryIdArray>
< / ol>

小提琴: http://jsfiddle.net/vSHQD/

解决方案

JSFiddle,当您将包装设置为onLoad或onDomready时,您定义的函数仅在该块内部定义,并且不能由外部事件处理程序访问。





  function something(...)

/ pre>

收件人:

  window.something = function(。 ..)


Check the Fiddle to see the failure occurring.

When I add Data (Even if I leave it empty) to the text box and try to click "Add" it doesn't do anything.

Opening the Chrome and Firefox console both give me the same error, it says "changeText2()" is not defined.

How can I fix this? I've ran into this error several times and mostly it had really weird workarounds, but I am not sure as to the method for avoiding it or what I'm even doing wrong.

It seems removing the global variable declarations fixes it most of the time, however, I need them in this case and would rather know why and how this error occurs.

Any and all help is greatly appreciated.

Javascript:

var list = document.getElementById('deliveryIdArray');
var names = [];

function changeText2() {
    var deliveryIdentification = document.getElementById('deliveryIdentification').value;
    names.push(deliveryIdentification);//simply add new name to array;
    //array changed re-render list
    renderList();
}

function renderList(){
    while (list.firstChild) {
        list.removeChild(list.firstChild);
    }
    //create each li again
    for(var i=0;i<names.length;i++){
        var entry = document.createElement('li');
        entry.appendChild(document.createTextNode(names[i]));
        var removeButton = document.createElement('button');
        removeButton.appendChild(document.createTextNode("remove"));
        removeButton.setAttribute('onClick','removeName('+i+')');
        entry.appendChild(removeButton);
        list.appendChild(entry);
    }
}


function removeName(nameindex){
    names.splice(nameindex,1);
    //array changed re-render list
    renderList();
}

function getDeliveries(){
    return names;
}

HTML:

<b>Number(s): </b>
    <input id = "deliveryIdentification" name = "deliveryIdentification" type = "text" size = "16" maxlength = "30">

    <!-- Array Area Creation -->
    <input type='button' onclick='changeText2()' value='Add' />

    <ol id="deliveryIdArray">
    </ol>

Fiddle: http://jsfiddle.net/vSHQD/

解决方案

In JSFiddle, when you set the wrapping to "onLoad" or "onDomready", the functions you define are only defined inside that block, and cannot be accessed by outside event handlers.

Easiest fix is to change:

function something(...)

To:

window.something = function(...)

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

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