Javascript 用字符串填充文本区域
我试图用java脚本中的字符串填充某个文本区域。我已经在函数中将字符串定义为变量“result”,并要求函数返回变量result。当它返回“result”时,我希望它在特定的文本区域中返回。因此,我使用document.getElementByID调用了文本区域,但它不会填充文本区域。我不确定我哪里做错了,或者从这里该往哪里走。任何帮助都将不胜感激。我已经包含了下面表单和函数的代码 JavaScriptJavascript 用字符串填充文本区域,javascript,forms,Javascript,Forms,我试图用java脚本中的字符串填充某个文本区域。我已经在函数中将字符串定义为变量“result”,并要求函数返回变量result。当它返回“result”时,我希望它在特定的文本区域中返回。因此,我使用document.getElementByID调用了文本区域,但它不会填充文本区域。我不确定我哪里做错了,或者从这里该往哪里走。任何帮助都将不胜感激。我已经包含了下面表单和函数的代码 JavaScript function newVerse(form1) { var objects = f
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>
对象:
目的地:
我做了一个有效的例子
以下是您的错误:
Javascript
HTML
物体:
目的地:
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>