“下一步/上一页”按钮抛出错误



当我尝试通过单击Next按钮插入已经输入的值时,我一直得到以下错误:

无法获取属性"0"的值:对象为空或未定义。

我认为错误发生在数组的最后一个值。我在代码中用注释指出了下面这行。我想让它获得数组中的下一个值,但还没有创建(如果下一个值不是数组中的最后一个值,它就可以获得下一个值)。

我认为这就是它抛出一个物体null的原因。但是,我似乎无法检查null/undefined并使用result[count+1][0] == undefined等语句设置它,因为它不起作用!无论我做什么,它总是抛出一个错误。

如果你能帮助我,我将不胜感激。

测试用例:

  1. 在文本框1和文本框2中插入值
  2. 单击Next
  3. 点击上一步(为了编辑上面插入的值)
  4. 将文本框中的值更改为其他值
  5. 点击下一步——错误发生
代码:

<html>
<head>
<script type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var result = new Array();
var count = 0;
var input1 = new Array();
var input2 = new Array();
function move(direction) {
if(direction == 'next') 
{
    var rate1 = [document.getElementById("txt1").value];
    var rate2 = [document.getElementById("txt2").value];

        if (result.length == count){
            if (rate1 == '' || rate2 == '') {
                alert('you need to put in a value');
            }
            else {
                result.push([[rate1], [rate2]]);
                document.getElementById("txt1").value = '';
                document.getElementById("txt2").value = '';
                count++;
            }
        }
        else {          
            try{
            (result[count][0]) = document.getElementById("txt1").value;
            (result[count][1]) = document.getElementById("txt2").value;
            document.getElementById("txt1").value = result[count++][0]; //error happening here. trying to show next value but there isn't one created yet. 
            document.getElementById("txt2").value = result[count++][1];
            document.getElementById("txt1").value = '';
            document.getElementById("txt2").value = '';
            }
            catch(err) {
            alert(err.description);
            }           
            count++;
        }   
}
if (direction == 'prev') 
{
    if(count <= 0)
    {
        alert("no more elements");
    }
    else 
    {
        var prev_val1 = (result[count - 1][0]);
        document.getElementById("txt1").value = prev_val1;
        var prev_val2 = (result[count - 1][1]);
        document.getElementById("txt2").value = prev_val2;
        count--;               
    }
}
document.getElementById("txtresult").value = result;
}
</script>
<li>text 1</li>
<input type="text" id="txt1"/>
<br>
<li>text 2</li>
<input type="text" id="txt2"/>
<br>
<input type="button" id="btn" value="next" onclick="move('next')" />
<input type="button" id="btnprevious" value="previous" onclick="move('prev')" />
<br>
<input type="text" id="txtresult"/>
</body>
</html>

您可以添加如下检查:

if (typeof result[count++] === "undefined") { /* do or do not */ };

:

document.getElementById("txt1").value = result[count++][0];
function move(direction) {
if(direction == 'next') 
{
    var rate1 = [document.getElementById("txt1").value];
    var rate2 = [document.getElementById("txt2").value];

        if (result.length == count){
            if (rate1 == '' || rate2 == '') {
                alert('you need to put in a value');
            }
            else {
                result.push([[rate1], [rate2]]);
                document.getElementById("txt1").value = '';
                document.getElementById("txt2").value = '';
                count++;
            }
        }
        else {          
            try{
            (result[count][0]) = document.getElementById("txt1").value;
            (result[count][1]) = document.getElementById("txt2").value;
            if( result[ ++count ] )  // this checks for undefined 
            {
                document.getElementById("txt1").value = result[count][0]; //error happening here. trying to show next value but there isn't one created yet. 
                document.getElementById("txt2").value = result[count][1];
            }
            else            
            {
                document.getElementById("txt1").value = '';
                document.getElementById("txt2").value = '';         
                count--;  // decrement counter 
            }
            }catch(err) {
            alert(err.description);
            }           
            count++;
        }   
}
if (direction == 'prev') 
{
    if(count <= 0)
    {
        alert("no more elements");
    }
    else 
    {
        var prev_val1 = (result[count - 1][0]);
        document.getElementById("txt1").value = prev_val1;
        var prev_val2 = (result[count - 1][1]);
        document.getElementById("txt2").value = prev_val2;
        count--;               
    }
}
document.getElementById("txtresult").value = result;
}

为什么在这两行中使用count++ ?

        document.getElementById("txt1").value = result[count++][0]; //error happening here. trying to show next value but there isn't one created yet. 
        document.getElementById("txt2").value = result[count++][1];

似乎解释器首先增加计数,然后尝试获得未定义的结果项…

据我所知,按previous必须"设置光标"到以前的值,以便您可以更改以前输入的值…在这种情况下,你不应该在这些行中增加计数器。删除++

我不明白你为什么把数组嵌入三层。我清理了一些代码,使名字更容易理解(至少对我来说)。

不管怎样,当你在处理数组中的最后一个值时,count++还不存在。另外,不要使用count++,因为这会增加你的count变量。不要使用++来简化,除非你真的知道你在做什么,并且想要增加。此外,复杂的快捷方式会让试图阅读代码的人感到困惑,因此请尽量明确。(这句话也有例外,比如,你不需要为一个从未编码过的人写代码)

这是工作javascript:

var result = new Array();
var count = 0;
function move(direction) {
    if(direction == 'next') {
        var box1 = document.getElementById("txt1").value; //why did you wrap these as arrays?
    var box2 = document.getElementById("txt2").value; //
    if (result.length == count){
        if (box1 == '' || box2 == '') {
            alert('you need to put in a value');
        } else {
            result.push([box1, box2]); //why did you wrap individual numbers in arrays?
            document.getElementById("txt1").value = '';
            document.getElementById("txt2").value = '';
        }
    } else {          
        try{
            result[count][0] = document.getElementById("txt1").value;  
            result[count][1] = document.getElementById("txt2").value;
            if(result[count+1]) { // need this because if on last value in the array, count+1 will not exist yet
                document.getElementById("txt1").value = result[count+1][0]; //do not do ++.  this will increment count here.  don't be tricky with ++
                document.getElementById("txt2").value = result[count+1][1]; //because it will confuse others and lead to off by 1 errors
            } else {
                document.getElementById("txt1").value = '';
                document.getElementById("txt2").value = '';
            }
        }
        catch(err) {
            alert(err.description);
        }           
    }
    count++;
}
if (direction == 'prev') {
    if(count <= 0){
        alert("no more elements");
    } else {
        var prev_val1 = result[count - 1][0];
        var prev_val2 = result[count - 1][1];
        document.getElementById("txt1").value = prev_val1;
        document.getElementById("txt2").value = prev_val2;
        count--;               
    }
}
document.getElementById("txtresult").value = result;
}

相关内容

  • 没有找到相关文章

最新更新