javascript字符串长度返回undefined

时间:2012-06-04 22:06:25

标签: javascript string casting undefined

我似乎无法从表单文本字段中的值获取字符串长度 这是我的函数的代码,它传递了textfields名称的参数

  function validate(thing)
  {

  var location = document.getElementById(thing);

  var cardst = location.value;
  window.alert (cardst);
  cardst = String(cardst);
  window.alert (cardst.lenght);

第一个警报有效,它会警告我在文本字段中输入的内容,但第二个警报始终未定义。正如你所看到的,我把它作为一个字符串投射但是我得到了未定义的......任何想法?

2 个答案:

答案 0 :(得分:11)

那是因为你拼写错误length: - )

请改用:

cardst.length;   // <- 'th', not 'ht'

答案 1 :(得分:1)

您遇到语法错误。它的长度不是长度

   function validate(thing)
  {

  var location = document.getElementById(thing);

  var cardst = location.value;
  window.alert (cardst);
  cardst = String(cardst);
  window.alert (cardst.lenght);
                           ^ /*should read window.alert(cardst.length)*/
相关问题