首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建提示框创建警报

创建提示框创建警报
EN

Stack Overflow用户
提问于 2016-07-28 10:56:53
回答 1查看 1.3K关注 0票数 3

我正在尝试使用Javascript创建表单。

我需要在form.Once字段中键入姓名和年龄,然后单击提交。提交需要创建一个提示,让您键入新的背景颜色。

一旦你点击确定我需要一个警告说(名字从字段名称“你最喜欢的颜色已应用到页面的背景”)你的年龄是(显示年龄从年龄字段)

例如: Brad你最喜欢的颜色被应用到页面的背景上。你已经33岁了。我不知道如何让javascript获取在name和age字段中输入的姓名和年龄。

HTML代码:

代码语言:javascript
复制
    <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脚本:

代码语言:javascript
复制
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) ");
  }
}   
EN

回答 1

Stack Overflow用户

发布于 2016-07-29 22:55:22

从DOM获取<input>元素的一种方法是在document.getElementById中使用它的id。从这个<input>获取输入文本的方法是通过它的.value属性。

因此,要从id为name的输入中获取字符串文本,您需要这样做

代码语言:javascript
复制
var name = document.getElementById("name").value;

这看起来可能是这样的:

代码语言:javascript
复制
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);
  }
}   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38626379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档