2

フォームのテキストフィールドの値から文字列の長さを取得できないようですこれが私の関数のコードで、テキストフィールド名のパラメータが渡されます

  function validate(thing)
  {

  var location = document.getElementById(thing);

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

最初のアラートは機能し、テキストフィールドに入力した内容をアラートしますが、2番目のアラートは常に未定義です。ご覧のとおり、文字列としてキャストしましたが、未定義になります。

4

2 に答える 2

14

スペルを間違えたためですlength:-)

代わりにこれを使用してください:

cardst.length;   // <- 'th', not 'ht'
于 2012-06-04T22:08:23.023 に答える
2

構文エラーがあります。長さではなく長さです

   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)*/
于 2012-06-04T22:09:34.923 に答える