我正在尝试使用Javascript创建表单。
我需要在form.Once字段中键入姓名和年龄,然后单击提交。提交需要创建一个提示,让您键入新的背景颜色。
一旦你点击确定我需要一个警告说(名字从字段名称“你最喜欢的颜色已应用到页面的背景”)你的年龄是(显示年龄从年龄字段)
例如: Brad你最喜欢的颜色被应用到页面的背景上。你已经33岁了。我不知道如何让javascript获取在name和age字段中输入的姓名和年龄。
HTML代码:
<form>
First name:<br>
<input type="text" name="firstname" id="name"><br>
Age:<br>
<input type="text" name="txtage" id="age"><br>
<input type="submit" name="submit" id="process" onclick="MyFunction">
</form>外部Java脚本:
function MyFunction() {
x = prompt("Enter the color you want on the Background???");
document.body.style.backgroundColor = x;
if (x != null){
alert("(need name from form)Your favorite color was applied to the background of the page, your age is (need age from form) ");
}
} 发布于 2016-07-29 22:55:22
从DOM获取<input>元素的一种方法是在document.getElementById中使用它的id。从这个<input>获取输入文本的方法是通过它的.value属性。
因此,要从id为name的输入中获取字符串文本,您需要这样做
var name = document.getElementById("name").value;这看起来可能是这样的:
function MyFunction() {
x = prompt("Enter the color you want on the Background???");
document.body.style.backgroundColor = x;
var name = document.getElementById("name").value;
var age = // ...get the age in a similar manner
if (x != null) {
// concat strings using the + operator
alert(name + "Your favorite color was applied to the background of the page, your age is " + age);
}
} https://stackoverflow.com/questions/38626379
复制相似问题