2011-11-11 50 views
0
function add(id) 
{ 
    var tempid=document.getElementById(id); 
    var patterm=/@/; 
    var value=tempid.match(patterm); // This is where I'm getting the error 
    if(value==null) 
    { 
     var length=document.getElementById(id).length(); 

     tempid=tempid.setchatAt(length+1,'@messung.com'); 
    } 
    else 
    { 
    } 
} 

回答

1

tempid是一個對象,您需要將其值與模式匹配。做一些像document.getElementById(id).value;

另外長度是屬性而不是方法。並且需要在字符串document.getElementById(id).value;上調用它。不在對象上。

+0

意味着不是temid我必須使用的document.getElementById(ID).value的; ?? – user1041240

+0

看到document.getElementById(id)它只是給你的對象。可以使用value屬性檢索對象的值。現在,因爲它看起來你想在價值上工作。是的,你必須這樣使用它。如果你嘗試提醒你在變量中獲得什麼,事情可能很簡單。像有價值和無價值的alert(tempid)。 –

1

在這一行上,您試圖對DOM對象執行字符串匹配,這將永遠不會工作。

var value=tempid.match(patterm); 

這可能不是你想要做的。如果這是一個輸入字段(它看起來像在測試電子郵件地址中的「@」),那麼您需要獲取輸入字段的值,而不僅僅是DOM對象。使用正則表達式搜索字符串中的一個字符也是低效的。這是你的功能的清理版本:

function add(id) 
{ 
    var val = document.getElementById(id).value; 
    // if no '@' in string, add default email domain onto the end 
    if (val.indexOf('@') == -1) 
    { 
     val += '@messung.com'; 
    } 
    else 
    { 

    } 
} 
0
function add(id) 
    { 
     var tempid=document.getElementById(id); 
     var patterm=/@/; 
     var value=tempid.value.match(patterm); // use value property of the Dom Object 
     if(value==null) 
     { 
      var length=tempid.value.length(); //Call lenght on the value of object 

      tempid.value = tempid.value.setchatAt(length+1,'@messung.com'); //set proper value 
     } 
     else 
     { 

     } 

    } 
+0

現在它顯示mw相同的錯誤爲var長度= temid.length(); – user1041240

+0

感謝它現在的工作 – user1041240

相關問題