javascript实现自动添加文本框功能

转自:http://www.cnblogs.com/damonlan/archive/2011/08/03/2126046.html

昨天,我们公司的网络小组决定为公司做一个内部的网站,主要是为员工比如发布公告啊、填写相应信息、投诉、问题等等需求。我那同事给了我以下需求:

1.点击一个按钮 就增加一个文本框。

2.把新建的文本框的名字命名为 questions[1] ,questions[2],questions[3]....这种形式。

3.可以删除,每次删除最后一个。

4.变色功能。就是当鼠标移入到一个文本框的时候,当前背景色自动的变成灰色。

其他 以后扩展再说。

先不说,上图为好,下面就是最终实现的效果。

整个过程不算太难理解,就是昨天晚上在整那个左边系号的时候 刚开始老是不对。后来整了一个全局变量,在进行判断一下就OK了。

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title><script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script><script type="text/javascript">var count = 1;//用来判断是删除 还是增加按钮 以便count值进行计算function checkCount(boolOK, coun) {if (boolOK == true) {return count++;}else {count--;}}//添加一个input标签 同时也对它的ID和Name进行赋值。function AddInput() {// checkCount(2, true);
            countAA = checkCount(true, count);// alert(countAA);//count++;var question = document.getElementById("question");//创建spanvar span = document.createElement("span");span.id = "lbl" + count;span.innerText = "您的第" + count + "个问题: ";question.appendChild(span);//创建inputvar input = document.createElement("input");input.type = "text";input.id = "questions[" + count + "]";input.name = "questions[" + count + "].name";question.appendChild(input);//创建一个空格var br = document.createElement("br");question.appendChild(br);}//每次删除最后一个input标签function DecInput() {var count2 = 0var inputs = document.getElementsByTagName("input");for (var i = 0; i < inputs.length; i++) {var input = inputs[i];if (input.type == "text") {count2++;}}var question = document.getElementById("question");var whichInput = document.getElementById("questions[" + count2 + "]");var whichSpan = document.getElementById("lbl" + count2 + "");question.removeChild(whichInput);question.removeChild(whichSpan);var brs = document.getElementsByTagName("br");question.removeChild(brs[count2 - 1]);checkCount(false, count2);}function TestClick() {var q2 = document.getElementById("questions[4]");if (q2) {alert("OK");}else {alert("No...");}}function initEvent() {var inputs = document.getElementsByTagName("input");for (var i = 0; i < inputs.length; i++) {var input = inputs[i];if (input.type == "text") {input.onmouseout = myOnmouseout;input.onfocus = myOnfocus;}}}function myOnmouseout() {this.style.backgroundColor = "white";}function myOnfocus() {this.style.backgroundColor = "gray";}</script>
</head>
<body onmousemove="initEvent()"><fieldset style="width: 500px; margin-left: 200px;"><legend><h6>亲爱的用户,请输入您的问题</h6></legend><div id="question" style="border: 1px solid red;"><span id="span1">您的第1个问题:</span><input id="Text1" type="text" /><br /></div><div style="margin-top: 100px;"><input id="btnAddInput" type="button" value="新增一个Input" onclick="AddInput()" /><input id="btnDecre" type="button" value="删除一个Input" onclick="DecInput()" /><input id="Button1" type="button" value="测试" onclick="TestClick()" /></div></fieldset>
</body>
</html>

 

转载于:https://www.cnblogs.com/tine/p/7448486.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391556.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从Mysql slave system lock延迟说开去

本文主要分析 sql thread中system lock出现的原因&#xff0c;但是笔者并明没有系统的学习过master-slave的代码&#xff0c;这也是2018年的一个目标&#xff0c;2018年我都排满了&#xff0c;悲剧。所以如果有错误请指出&#xff0c;也作为一个笔记用于后期学习。同时也给出笔…

传智播客全栈_播客:从家庭学生到自学成才的全栈开发人员

传智播客全栈In this weeks episode of the freeCodeCamp podcast, Abbey chats with Madison Kanna, a full-stack developer who works remotely for Mediavine. Madison describes how homeschooling affected her future learning style, how she tackles imposter syndrom…

leetcode 82. 删除排序链表中的重复元素 II(map)

解题思路 map记录数字出现的次数&#xff0c;出现次数大于1的数字从链表中移除 代码 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(…

python 列表、字典多排序问题

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 by-sa 版权协议&#xff0c;转载请附上原文出处链接和本声明。本文链接&#xff1a;https://blog.csdn.net/justin051/article/details/84289189Python使用sorted函数来排序&#xff1a; l [2,1,3,5,7,3]print…

接facebook广告_Facebook广告分析

接facebook广告Is our company’s Facebook advertising even worth the effort?我们公司的Facebook广告是否值得努力&#xff1f; 题&#xff1a; (QUESTION:) A company would like to know if their advertising is effective. Before you start, yes…. Facebook does ha…

如何创建自定义进度栏

Originally published on www.florin-pop.com最初发布在www.florin-pop.com The theme for week #14 of the Weekly Coding Challenge is: 每周编码挑战第14周的主题是&#xff1a; 进度条 (Progress Bar) A progress bar is used to show how far a user action is still in…

基于SpringBoot的CodeGenerator

title: 基于SpringBoot的CodeGenerator tags: SpringBootMybatis生成器PageHelper categories: springboot date: 2017-11-21 15:13:33背景 目前组织上对于一个基础的crud的框架需求较多 因此选择了SpringBoot作为基础选型。 Spring Boot是由Pivotal团队提供的全新框架&#xf…

seaborn线性关系数据可视化:时间线图|热图|结构化图表可视化

一、线性关系数据可视化lmplot( ) 表示对所统计的数据做散点图&#xff0c;并拟合一个一元线性回归关系。 lmplot(x, y, data, hueNone, colNone, rowNone, paletteNone,col_wrapNone, height5, aspect1,markers"o", sharexTrue,shareyTrue, hue_orderNone, col_orde…

hdu 1257

http://acm.hdu.edu.cn/showproblem.php?pid1257 题意&#xff1a;有个拦截系统&#xff0c;这个系统在最开始可以拦截任意高度的导弹&#xff0c;但是之后只能拦截不超过这个导弹高度的导弹&#xff0c;现在有N个导弹需要拦截&#xff0c;问你最少需要多少个拦截系统 思路&am…

eda可视化_5用于探索性数据分析(EDA)的高级可视化

eda可视化Early morning, a lady comes to meet Sherlock Holmes and Watson. Even before the lady opens her mouth and starts telling the reason for her visit, Sherlock can tell a lot about a person by his sheer power of observation and deduction. Similarly, we…

我的AWS开发人员考试未通过。 现在怎么办?

I have just taken the AWS Certified Developer - Associate Exam on July 1st of 2019. The result? I failed.我刚刚在2019年7月1日参加了AWS认证开发人员-联考。结果如何&#xff1f; 我失败了。 The AWS Certified Developer - Associate (DVA-C01) has a scaled score …

关系数据可视化gephi

表示对象之间的关系&#xff0c;可通过gephi软件实现&#xff0c;软件下载官方地址https://gephi.org/users/download/ 如何来表示两个对象之间的关系&#xff1f; 把对象变成点&#xff0c;点的大小、颜色可以是它的两个参数&#xff0c;两个点之间的关系可以用连线表示。连线…

Hyperledger Fabric 1.0 从零开始(十二)——fabric-sdk-java应用

Hyperledger Fabric 1.0 从零开始&#xff08;十&#xff09;——智能合约&#xff08;参阅&#xff1a;Hyperledger Fabric Chaincode for Operators——实操智能合约&#xff09; Hyperledger Fabric 1.0 从零开始&#xff08;十一&#xff09;——CouchDB&#xff08;参阅&a…

css跑道_如何不超出跑道:计划种子的简单方法

css跑道There’s lots of startup advice floating around. I’m going to give you a very practical one that’s often missed — how to plan your early growth. The seed round is usually devoted to finding your product-market fit, meaning you start with no or li…

将json 填入表格_如何将Google表格用作JSON端点

将json 填入表格UPDATE: 5/13/2020 - New Share Dialog Box steps available below.更新&#xff1a;5/13/2020-下面提供了 新共享对话框步骤。 Thanks Erica H!谢谢埃里卡H&#xff01; Are you building a prototype dynamic web application and need to collaborate with …

leetcode 173. 二叉搜索树迭代器

实现一个二叉搜索树迭代器类BSTIterator &#xff0c;表示一个按中序遍历二叉搜索树&#xff08;BST&#xff09;的迭代器&#xff1a; BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在…

jyputer notebook 、jypyter、IPython basics

1 、修改jupyter默认工作目录&#xff1a;打开cmd&#xff0c;在命令行下指定想要进的工作目录&#xff0c;即键入“cd d/ G:\0工作面试\学习记录”标红部分是想要进入的工作目录。 2、Tab补全 a、在命令行输入表达式时&#xff0c;按下Tab键即可为任意变量&#xff08;对象、…

cookie和session(1)

cookie和session 1.cookie产生 识别用户 HTTP是无状态协议&#xff0c;这就回出现这种现象&#xff1a;当你登录一个页面&#xff0c;然后转到登录网站的另一个页面&#xff0c;服务器无法认识到。或者说两次的访问&#xff0c;服务器不能认识到是同一个客户端的访问&#xff0…

熊猫数据集_为数据科学拆箱熊猫

熊猫数据集If you are already familiar with NumPy, Pandas is just a package build on top of it. Pandas provide more flexibility than NumPy to work with data. While in NumPy we can only store values of single data type(dtype) Pandas has the flexibility to st…

2018年,你想从InfoQ获取什么内容?丨Q言Q语

- Q 言 Q 语 第 三 期 - Q言Q语是 InfoQ 推出的最新板块&#xff0c; 旨在给所有 InfoQer 一个展示观点的平台。 每期一个主题&#xff0c; 不扣帽子&#xff0c;不论对错&#xff0c;不看输赢&#xff0c; 只愿跟有趣的灵魂相遇。 本期话题&#xff1a; 2018年&#xff0c;你想…