2011-09-20 38 views
0

我不斷收到以下錯誤,當我嘗試通過單擊在已經進入的價值觀下一步按鈕插入值。下一頁/上一頁按鈕拋出錯誤

無法獲取財產「的價值0 ':object是null或undefined。

我相信錯誤發生在數組中的最後一個值。我在代碼中註釋了下面一行代碼。我希望它獲得數組中的下一個值,但還沒有創建一個(如果下一個值不是數組中的最後一個值,它會得到下一個值就好了)。

我認爲這是拋出一個對象null的原因。但是,我似乎無法檢查null/undefined並使用諸如result[count+1][0] == undefined之類的語句進行設置,因爲它不起作用!無論我做什麼,它總會拋出一個錯誤。

一些幫助將不勝感激。

測試用例:

  1. 插入在文本框1和文本框2
  2. 單擊下一頁的值
  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> 
+0

你找到答案嗎?它有用嗎? –

回答

0

您可以添加一個檢查這樣的:

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

權利之前:

document.getElementById("txt1").value = result[count++][0]; 
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; 
} 
0

你爲什麼這樣做算++在這2條線?

 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]; 

似乎是解釋第一遞增計數,然後嘗試獲得的結果是不確定的項目...

我按已瞭解以前必須「設置光標」以前vaues這樣你就可以改變以前輸入的值...在這種情況下,你不應該在這些行中增加計數器..只是刪除++

+0

哎呀抱歉,我的意思是寫結果[count + 1] [0],而不是++ ++ ....上面的計數++是我早前搞亂的東西... – dekij

0

我不明白你爲什麼嵌入數組三深。我清理了一些代碼,並使得這些名稱更易於理解(至少對我來說)。

無論如何,當你在數組中的最後一個值時,count ++不存在。另外,不要使用count ++,因爲這會增加你的count var。除非你真正知道你在做什麼並且想要增加,否則不要使用++來簡化。另外,棘手的捷徑會讓試圖閱讀你的代碼的人感到困惑,所以儘量做到儘可能明確。(也有例外這一說法,作中,你不需要寫誰之前從來沒有編碼的人)

這裏是工作的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; 
} 
+0

啊非常感謝!從你的帖子中學到很多東西!現在完美工作 – dekij

相關問題