2013-10-28 38 views
0

我寫了一個簡單的函數,在每次更新一個文本區域時執行,但我想要獲取該文本區域中文本的最後一個字符。
爲了做到這一點,我使用了charAt,但代碼停止工作。爲什麼我的charAt函數不能在我的javascript上工作​​?

請別人告訴我我犯了什麼錯誤。

// array is goes here 

function startText() { 
    var str=document.getElementById("intxt").value; 
    var matchkeyword = str.charAt(str.length-1); 
    // code extends 

    var sugest = ""; 

    for (var i = 0; i < consonants.length; i++) { 
     if (consonants[i].match(matchkeyword.value) != null && matchkeyword.value != "") { 
      sugest = sugest + "<br>" + consonants[i] + "=" + consonantsUni[i]; 
     } 
    } 
    y = document.getElementById("sugdiv"); 
    y.innerHTML = sugest; 
} 
+0

「不工作」不是描述問題;告訴我們會發生什麼。並在問題中包含相關的HTML代碼。 –

回答

2

像這樣

var matchkeyword = str.charAt(str.length-1); 

賦值給一個變量後,你不應該matchkeyword使用value。相反的,

if(consonants[i].match(matchkeyword.value) != null && matchkeyword.value != "") { 

使用

if(consonants[i].match(matchkeyword) != null && matchkeyword != "") { 

注:如果您使用的是Chrome,只要劇本 「停止工作」,按F12這將打開開發人員工具。你將能夠從中得到線索。

相關問題