通过js来动态添加文本框的行数,这个特效还带有删除功能,根据自己的需求来自定义即可。
<html> <head> <meta http-equiv="content-Type" content="text/html;charset=gb2312"> <meta name="keywords" content="武鸣人网站,武鸣信息网,武鸣本地网,武鸣信息资源平台,减肥,健身,励志,励志语录,js特效,网页特效,www.wuming.ren"> <meta name="description" content="欢迎来到武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢,分享互联网流行的励志语录经典短句,减肥健身健康小常识及打卡记录,收集各种js特效代码。"> <title>js代码实现动态添加文本框行数效果_武鸣人</title> </head> <body> <a href="https://www.wuming.ren">武鸣人</a>,各种信息资源免费发布,分享励志语录经典短句,减肥健身常识,各种js特效代码。网站很好记住,wuming.ren(武鸣拼音.ren域名后缀)<hr> <!--欢迎来到武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢,分享互联网流行的励志语录经典短句,减肥健身健康小常识及打卡记录,收集各种js特效代码。--> <script type="text/javascript" src="https://www.wuming.ren/ad/tc.js"></script> <script type="text/javascript" src="https://www.wuming.ren/ad/a.js"></script> <script language="javascript"> // Last updated 2006-02-21 function addRowToTable() { var tbl = document.getElementById('tblSample'); var lastRow = tbl.rows.length; // if there's no header row in the table, then iteration = lastRow + 1 var iteration = lastRow; var row = tbl.insertRow(lastRow); // left cell var cellLeft = row.insertCell(0); var textNode = document.createTextNode(iteration); cellLeft.appendChild(textNode); // right cell var cellRight = row.insertCell(1); var el = document.createElement('input'); el.type = 'text'; el.name = 'txtRow' + iteration; el.id = 'txtRow' + iteration; el.size = 40; el.onkeypress = keyPressTest; cellRight.appendChild(el); // select cell var cellRightSel = row.insertCell(2); var sel = document.createElement('select'); sel.name = 'selRow' + iteration; sel.options[0] = new Option('text zero', 'value0'); sel.options[1] = new Option('text one', 'value1'); cellRightSel.appendChild(sel); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) function keyPressTest(e, obj) { var validateChkb = document.getElementById('chkValidateOnKeyPress'); if (validateChkb.checked) { var displayObj = document.getElementById('spanOutput'); var key; if(window.event) { key = window.event.keyCode; } else if(e.which) { key = e.which; }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) var objId; if (obj != null) { objId = obj.id; } else { objId = this.id; } displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key); } } function removeRowFromTable() { var tbl = document.getElementById('tblSample'); var lastRow = tbl.rows.length; if (lastRow > 2) tbl.deleteRow(lastRow - 1); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) function openInNewWindow(frm) { // open a blank window var aWindow = window.open('', 'TableAddRowNewWindow', 'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400'); // set the target to the blank window frm.target = 'TableAddRowNewWindow'; // submit frm.submit(); } function validateRow(frm) { var chkb = document.getElementById('chkValidate'); if (chkb.checked) { var tbl = document.getElementById('tblSample'); var lastRow = tbl.rows.length - 1; var i; for (i=1; i<=lastRow; i++) { var aRow = document.getElementById('txtRow' + i); if (aRow.value.length <= 0) { alert('Row ' + i + ' is empty'); return; } } } openInNewWindow(frm); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) </script> <form method="get" action=""> <p> <input type="button" value="添加" onclick="addRowToTable();" /> <input type="button" value="删除" onclick="removeRowFromTable();" /> <input type="button" value="提交" onclick="validateRow(this.form);" /> <input type="checkbox" id="chkValidate" /> Validate Submit </p> <p> <!--<input type="checkbox" id="chkValidateOnKeyPress" checked="checked" /> Display OnKeyPress --> <span id="spanOutput" style="border: 1px solid #000; padding: 3px;"> </span> </p> <table border="1" id="tblSample"> <tr> <th colspan="3">添加选项</th> </tr> <tr> <td>1</td> <td><input type="text" name="txtRow1" id="txtRow1" size="40" /></td> <td> <select name="selRow0"> <option value="value0">text zero</option> <option value="value1">text one</option> </select> </td> </tr> </table> </body> </html>
本文来自武鸣人网站,转载请注明出处