如何循环输入类型文本
How to loop the input type text?
如何循环输入类型文本,使其具有自己唯一的属性,例如名称,值。我的意思是,例如 name="text1", name="text2",.类似的东西。这是我的代码。谢谢:)
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="javascript">
function add(type) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("value", "typhere");
element.setAttribute("name", "txtbox");
var btns = document.createElement("input");
btns.setAttribute("type", "button" );
btns.setAttribute("value", "delete");
btns.setAttribute("name", "dlete");
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
foo.appendChild(btns);
var br = document.createElement("br");
foo.appendChild(br);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<H2></H2>
<BR/>
<INPUT type="button" value="Add" onclick="add(document.forms[0].value);"/>
<span id="fooBar"><br/></span>
</FORM>
</BODY>
</HTML>
你可以这样做:
var inputId = 0;
function add(type){
// do the stuff you have to do with inputId
// input.setAttribute("name", "text" + inputId); for example
inputId++;
}
如果你不想污染全局命名空间,你可以这样做:
(function(window){
var inputId = 0;
window.InputManager = {
add : function(type){
// do tuff with inputId
// input.setAttribute("name", "text" + inputId); for example
inputId++;
}
};
})(window);
然后
<input type="button" value="Add" onclick="InputManager.add(document.forms[0].value)"/>
试试这个:
var counter=1;
function add(type) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("value", "typhere"+counter);
element.setAttribute("name", "txtbox"+counter);
var btns = document.createElement("input");
btns.setAttribute("type", "button" );
btns.setAttribute("value", "delete");
btns.setAttribute("name", "dlete");
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
foo.appendChild(btns);
var br = document.createElement("br");
foo.appendChild(br);
counter++;
}
演示:http://jsfiddle.net/kLJWW/
var i=1;
function add(type) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element
element.setAttribute("type", "text");
element.setAttribute("value", "typhere"+i);
element.setAttribute("name", "txtbox"+i);
var btns = document.createElement("input");
btns.setAttribute("type", "button" );
btns.setAttribute("value", "delete"+i);
btns.setAttribute("name", "dlete"+i);
i++;
使用变量 i 递增值。
相关文章:
- 在<输入类型=“;文件“/>
- 如何将输入类型值或id从一个jsp传递到另一个jsp页面
- 可以't将几个数字设置为<输入类型=“;数字“>
- 在输入类型日期中设置日期
- 如何将键入的文本从一个输入类型的文本复制到另一个
- 输入类型按钮返回历史记录并返回顶部
- 从HTML表单发布blob的表单输入类型是什么
- 输入类型文件的未捕获类型错误
- 如何在jQuery Mobile中设置100%宽度的输入类型日期
- 输入类型=文件验证停止其他输入类型验证
- 如何显示在输入类型文件中选择的文件
- jQuery Modal Popup-回发后输入类型设置为null
- 在控制器中将输入类型时间更改为秒
- 更改具有相同id的输入类型的所有值
- 对于每个输入类型,使用javascript获取值
- 输入类型范围在angularjs重复内不起作用
- 我如何使HTML5<输入类型=月份>以在所有浏览器上工作
- 如何调用两个函数是一种html输入类型
- 如何使按钮作为输入类型文件
- 可以设置输入类型=文件的文本框的高度