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.pswp.cn/news/391556.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391556.shtml
英文地址,請注明出處:http://en.pswp.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;你想…