簡體   English   中英

TypeError:d.textarea1未定義,d.textarea1.value = sChoices;

[英]TypeError: d.textarea1 is undefined , d.textarea1.value = sChoices;

嗨,我有這個javascript代碼,當我在選擇下拉列表中單擊所需的質量時,它將顯示到文本區域。 現在,我收到此錯誤TypeError:d.textarea1未定義d.textarea1.value = sChoices;

我的代碼有問題嗎?

<!DOCTYPE html>
<html>
<head>
  <title>List Box 4 Example</title>
  <script type="text/javascript">
    function tellMe(d){
      var sChoices ="";
      for(i=0; i<d.listbox.options.length; i++){
        if(d.listbox.options[i].selected == true){
          sChoices += d.listbox.options[i].text +"\n";
        }
      }
      d.textarea1.value = sChoices;
    }
  </script>
</head>
<body bgcolor="lightgreen">
  <form name="form1">
    <p>Girl's quaalities you want?</p>
    <select name="listbox" onchange="tellMe(this.form)" multiple>
      <option>Pretty</option>
      <option>Sexy</option>
      <option>Hot</option>
      <option>Intelligent</option>
      <option>Funny</option>
    </select>
  </form>
  <br />
  You Choose:<br />
  <textarea rows="4" cols="20" name="textarea1"></textarea>
</body>
</html>

非常感謝任何幫助! 謝謝

textarea不在表單中,因此您將無法從表單元素中引用它,只需將其添加到表單中即可。

  <form name="form1">
    <p>Girl's quaalities you want?</p>
    <select name="listbox" onchange="tellMe(this.form)" multiple>
      <option>Pretty</option>
      <option>Sexy</option>
      <option>Hot</option>
      <option>Intelligent</option>
      <option>Funny</option>
    </select>
  <br />
  You Choose:<br />
  <textarea rows="4" cols="20" name="textarea1"></textarea>  
</form>

http://jsfiddle.net/TgV83/

您還應該將textarea包裹在表單內,否則d.textarea1對於您的js代碼d.textarea1是未知的,因為d是您的表單,而textarea不在表單之外。 有關工作代碼,請參見Musa的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM