2016-11-13 115 views
-2

如何使用Javascript预设表格中的值?打印到文本区域

<u><h1>Rabbits vs Foxes Population Model</h1></u> 
    <form name = "Rabbits"> 
    <table border = "3"> 
    <tr><td>No. of Rabbits</td><td><center><input type="number" id="no_rabbits" /></center></td></tr> 
    <tr><td>Death Rates</td><td><center><input type="number" id="death" /></center></td></tr> 
    <tr><td>Birth Rates</td><td><center><input type="number" id="birth" /></center></td></tr> 
    <tr><td>No. of Foxes</td><td><center><input type="number" id="foxes" /></center></td></tr> 
    <tr><td>Rabbit Conditions</td><td> Normal <input type="radio" name="condition" value="normal"> Cold <input type="radio" name="condition" value="cold"> Fires <input type="radio" name="condition" value="fires">Excellent <input type="radio" name="condition" value="excellent"></td></tr> 
    <tr><td> Graph the Rabbits</td><td><input type="checkbox" name="graph" value="answer"></td></tr> 
    <tr><td><button type = "button" onclick="addyear()"> One Year Passes </button></td><td> </td></tr> 
    </table> 
    </form> 
+0

预设从源头什么什么样的价值观?可能值得花几分钟阅读[问]然后更新与所有相关的细节问题 – charlietfl

+0

哦对不起,这是我的第一个问题。如果我在JavaScript中设置了值。 –

+0

在'javascript'处的''元素中的三个都有一个'.value'集合。 'document.getElementById(“no_rabbits”)。value =“abc'; – guest271314

回答

1

声明输入时您可以设置的值(例如:<input type="number" id="no_rabbits" value="23"/>)或用JavaScript做到这一点和目标使用每个ID的输入和简单地设置该值。请注意,我也改变了你的h1结构 - 对于h1应用css规则要好得多,而不是h1的。不知道每个值是什么 - 只需设置为1 - 4就可以了。另外addYear函数没有声明,因此在点击时发生错误。我也删除了<center>标签 - 它们不再被支持,你应该使用CSS来为所需的td应用居中CSS规则。

另一件事 - 即使这些输入设置为type =“number”,它们仍然会返回一个字符串 - 因此您需要解析它们(使用parseInt或parseFloat)以获取计算的数字。

document.getElementById('no_rabbits').value="1"; 
 
document.getElementById('death').value="2"; 
 
document.getElementById('birth').value="3"; 
 
document.getElementById('foxes').value="4";
h1{text-decoration: underline}
<h1>Rabbits vs Foxes Population Model</h1> 
 
    <form name = "Rabbits"> 
 
    <table border = "3"> 
 
    <tr><td>No. of Rabbits</td><td><input type="number" id="no_rabbits" /></td></tr> 
 
    <tr><td>Death Rates</td><td><input type="number" id="death" /></td></tr> 
 
    <tr><td>Birth Rates</td><td><input type="number" id="birth" /></td></tr> 
 
    <tr><td>No. of Foxes</td><td><input type="number" id="foxes" /></td></tr> 
 
    <tr><td>Rabbit Conditions</td><td> Normal <input type="radio" name="condition" value="normal"> Cold <input type="radio" name="condition" value="cold"> Fires <input type="radio" name="condition" value="fires">Excellent <input type="radio" name="condition" value="excellent"></td></tr> 
 
    <tr><td> Graph the Rabbits</td><td><input type="checkbox" name="graph" value="answer"></td></tr> 
 
    <tr><td><button type = "button" onclick="addyear()"> One Year Passes </button></td><td> </td></tr> 
 
    </table> 
 
    </form>

+0

哦,我现在看到了,谢谢! –

+0

欢迎您 - 很高兴为您提供帮助。查看我的答案 - 我已经编辑了,因为您最后一次查看了这个答案。 “接受”复选框,以便其他用户可以看到它有一个解决方案。Cheers Gav。 – gavgrif