如何使在文本字段中输入的值显示在HTML语句中
How to make the values entered in a text field appear in the HTML sentence
我正在做一项作业,我必须随机组成10个句子,并创建文本字段。这些文本字段应该包括我在句子中放入的内容。我需要帮助编写包含句子中单词的代码。我想我已经想好了怎么做,除非我在"name"中键入一些东西时,它只显示我输入的一个字母,而不是整个单词。
这是我的HTML:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Assignment 4</title>
<h1> Assignment 4 - Sample Solution </h1>
</head>
<body>
<p>
Name: <input type="text" id = "input" value= "names" size="10" />
<br />
Verb Phrase: <input type="text" id = "input2" value="verbs" size="10" />
<br />
Adjective: <input type="text" id = "input3" value="adjs" size="10" />
<br />
Noun: <input type="text" id = "input4" value="nouns" size="10" />
<br />
HOW MANY SENTENCES? <select id = "numOfSentences" size = "1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<button id = "displaySilly">
Display Silly
</button>
<p id = "output"></p>
<script src = "silly.js"></script>
</body>
</html>
这是我的JS。
//arrays containing values for sentence components
var names = ["Alice","Rowena","Carol","David","Erin"];
var verbs = ["jumped on", "ran from", "scolded", "yelled at","talked to"];
var adjs = ["yellow","big","smelly","hairy","bad"];
var nouns = ["bear","tree","rock","student","instructor"];
// variables for the sentence components
var name, verb, adj, noun;
//when window loads
window.onload = function () {
var names = document.getElementById("input");
names.value = null;
var verbs = document.getElementById("input2");
verbs.value= null;
var adjs = document.getElementById("input3");
adjs.value= null;
var nouns = document.getElementById("input4");
nouns.value= null;
}
// display what is put into the text fields in the sentences
//names.push(document.getElementById('input').value);
//verbs.push(document.getElementById('input2').value);
//adjs.push(document.getElementById('input3').value);
//nouns.push(document.getElementById('input4').value);
// display silly sentences
document.getElementById('displaySilly').onclick = function() {
var names = document.getElementById("input").value;
document.getElementById("output").innerHTML = names;
// get number of sentences from drop down
var numOfSentences =
document.getElementById('numOfSentences').value;
//convert to integer
numOfSentences = parseInt(numOfSentences);
// initialize results string
var results = "";
// create required number of silly sentences
for (var i = 1 ; i <= numOfSentences ; i++) {
//pick components at random from arrays
name =
names[Math.floor(Math.random() * names.length)];
verb =
verbs[Math.floor(Math.random() * verbs.length)];
adj =
adjs[Math.floor(Math.random() * adjs.length)];
noun =
nouns[Math.floor(Math.random() * nouns.length)];
// concatenate to form a sentence
// add to other sentences
results = results + name + " " + verb +
" the " + adj + " " + noun +
".<br />";
}
// display the silly sentences
document.getElementById('output').innerHTML = results;
} //*** END onclick handler
我需要的帮助
var names = document.getElementById("input").value;
document.getElementById("output").innerHTML = names;
谢谢各位
具有相同标识符的不同变量;例如三个称为CCD_ 1的变量。在接近尾声时,当你用从阵列中读取时
names[Math.floor(Math.random() * names.length)];
与包含名称的数组不是同一个names
!
因此,解决方案是给所有这些names
取不同的名称。
相关文章:
- Brightcove获取/显示HTML中的当前视频标题和描述
- HTML 标记未在 AngularJS ckEditor 中应用,而是在 Ediator 中显示 HTML 元素标记代码
- 使用jqLite隐藏和显示html元素
- 如何在不打断标记的情况下突出显示html字符串中的文本
- JS来显示HTML标记
- 如果javascript打开/关闭,则隐藏和显示html代码
- 用javascript显示html表
- 显示HTML代码块
- Regex有助于突出显示html中匹配文本的角度过滤器
- 为什么WebBrowser控件在保存后不能正确显示HTML
- 如何通过ibmworklight中的sql适配器从db2中检索或显示html页面上的数据?android混合应用程序开发
- 如何使用window.showModalDialog()显示HTML内容
- JavaScript if else 语句来显示 html 标签
- 使用 js 函数显示 HTML 表单的结果
- 如何在不点击的情况下突出显示 HTML 中的文本
- 如何隐藏和显示 html 输入文本和段落
- 使用 javascript 解析和显示 HTML 音频当前时间
- 有什么方法可以在HTML页面中显示HTML代码吗
- 在自动完成jqueryUi中显示html
- 单击并用CTRL+C复制值时高亮显示html表格单元格