2011-11-11 55 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

相关问题