0

我正在尝试用 java 脚本中的字符串填充某个文本区域。我已经在我的函数中将字符串定义为变量“result”,并让函数返回变量结果。当它返回“结果”时,我希望它在特定的文本区域中返回。所以我使用 document.getElementByID 调用了文本区域,但它不会填充文本区域。我不确定我在哪里出错或从这里去哪里。任何帮助将不胜感激。我在下面包含了表单和函数的代码。

JavaScript

function newVerse(form1) {
    var objects = form1.objects.value;
    var destination = form1.destination.value;
    var result = "Where have all the" + objects + "gone?" + "Long time passing." + "Where have all the" + objects + "gone?" + "Long time ago." + "Where have all the" + objects + "gone?" + "Gone to" + destination + ", everyone." + "When will they ever learn?" + "When will they ever learn?";
    document.getElementByID(textarea).value += result
    return result;
}

HTML

<form name=form1>Objects:
    <input type="text" name="objects">
    <br>Destination:
    <input type="text" name="destination">
    <br>
    <input type="button" name="submit" value="Submit" onclick="newVerse(form1)">
</form>
4

1 回答 1

0

我在这里做了一个工作示例。

这是你的错误:

Javascript

function newVerse(form1) { 
  var objects = form1.objects.value;
  var destination = form1.destination.value;
  var result = "Where have all the" + objects + "gone?" + "Long time passing." + "Where have all the" + objects + "gone?" + "Long time ago." + "Where have all the" + objects + "gone?" + "Gone to" + destination + ", everyone." + "When will they ever learn?" + "When will they ever learn?";

  //Missing quotes arround text area.
  //Missing ; althought is not necessary.
  //document.getElementByID(textarea).value += result
   document.getElementByID('textarea').value += result;
}

HTML

<form name=form1><!-- Missing "" around form1 -->
  Objects:
  <input type="text" name="objects"> <!-- Missing closing / -->
  <br>Destination:
  <input type="text" name="destination"> <!-- Missing closing / -->
  <br>
  <input type="button" name="submit" value="Submit" onclick="newVerse(form1)">
  <!-- You want to pass the actual form, so replace onclick="newVerse(form1)" by onclick="newVerse(document.forms['form1'])"-->
  <!-- Missing closing / -->
</form>
于 2013-11-03T20:16:41.923 回答