我正在尝试使用java脚本中的字符串填充某个文本区域。我已经在我的函数中将字符串定义为变量“result”,并且表示函数返回变量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>
答案 0 :(得分:0)
我做了一个有效的例子here。
以下是您的错误:
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;
}
<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>