Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回未定义的javascript字符串长度_Javascript_String_Casting_Undefined - Fatal编程技术网

返回未定义的javascript字符串长度

返回未定义的javascript字符串长度,javascript,string,casting,undefined,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);

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

  function validate(thing)
  {

  var location = document.getElementById(thing);

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

第一个警报有效,它会提醒我在文本字段中键入的任何内容,但第二个警报始终未定义。如你所见,我确实将其转换为字符串,但我没有定义。。有什么想法吗???

那可能是因为你拼错了
长度:-)

改用这个:

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

cardst.length;// 您有一个语法错误。这是长度而不是长度

   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)*/

打字错误:
cardst.length
应该是
cardst.length
;另外,不确定创建新字符串的原因。使用“location”作为变量名是个坏主意。如何调用函数?在大多数浏览器上尝试ctrl+shift+j