我在外部 js 文件中构建了函数,我正在尝试创建一个按钮来运行该函数,然后将结果插入到文本框中。我已经把头撞到墙上几个小时了,我觉得我有点接近了。
HTML 代码:
<label for="fName">First Name </label>
<input type="text" name="fName" id="fName" /><p>
<input type="button" value="JS Test" name="button1" onclick="writeHumanName(this.form)" />
JS代码:
function writeHumanName() {
// Array of my family
var humanName = ["Troy", "Beth", "Gabby", "Owen", "Evan"];
document.write("My Family = ");
// Loop through the humanName array
for (var i = 0; i < humanName.length; i++) {
document.write(humanName[i]);
}
}