如何从html中动态创建的文本框中获取价值 [英] how to get value from dynamically created textbox in html
本文介绍了如何从html中动态创建的文本框中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从html中动态创建的文本框中获取价值。但是我不能......请求帮助。
I want to get value from dynamically created textbox in html. but I can't.. plez help.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Matrix for Life</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#newtable{
border-left:2px solid #999;
border-right:2px solid #999;
font-family:verdana,arial,helvetica,sans-serif;
font-size:18px;
margin:auto;
}
input.node{
hight: 45px;
width: 55px;
border:1px doted #999;
}
MatA{
}
</style>
<script type="text/javascript">
function AddMatrix() {
row=new Array();
cell=new Array();
row_num=document.getElementById("noofrows").value;
cell_num=document.getElementById("noofcols").value;
tab=document.createElement('table');
tab.setAttribute('id','newtable');
tbo=document.createElement('tbody');
for(c=0;c<row_num;c++){
row[c]=document.createElement('tr');
for(k=0;k<cell_num;k++) {
cell[k]=document.createElement('td');
var input = document.createElement("input");
input.type = "text";
input.name = 'node'+c+','+k;
input.setAttribute('class','node');
cell[k].appendChild(input);
row[c].appendChild(cell[k]);
}
tbo.appendChild(row[c]);
}
tab.appendChild(tbo);
document.getElementById('mytable').appendChild(tab);
}
function show(){
var val = document.getElementById("mytable").rows[0].cells[0].getElementsByTagName("input").value;
alert(val);
}
</script>
</head>
<body>
<div>
<h3>Matrix</h3>
Number of Rows: <input type="text" id="noofrows" value='2' />
Number of Colomns: <input type="text" id="noofcols" value='3'/>
<input type="button" id="add" value="Add" onclick="AddMatrix()"/>
</div>
<div id="mytable"></div>
<input type="button" id="show" value="Show" onclick="show()"/>
</body>
</html>
推荐答案
我认为你应该为文本框提供ids)属性,而且模式也是如此。
就像你有三个文本框一样,分别使用ids tb1,tb2,tb3。
然后用tb4填充dynamicaly创建的文本框的id属性(其中4来自某个计数器)
值或者你可以从getElementsByTagName(输入)得到它。长度。
Finaly ,
通过getElementBy Id(tb+ counter).value = someValue你可以为动态创建的文本框插入值。
最好的运气..
I think you should provide the ids)property) to the textboxes and that's too in pattern.
Like if you have three textboxes initialy with ids tb1 , tb2,tb3 respectively.
Then fill the id attribute of dynamicaly created textbox with tb4(where 4 is from some counter
value or you may get it from getElementsByTagName("input").length).
Finaly,
through getElementById("tb"+counter).value=someValue you can insert value to the dynamicaly created textbox.
Best Of Luck..
>
试试这段代码。
try this code.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Matrix for Life</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#newtable{
border-left:2px solid #999;
border-right:2px solid #999;
font-family:verdana,arial,helvetica,sans-serif;
font-size:18px;
margin:auto;
}
input.node{
hight: 45px;
width: 55px;
border:1px doted #999;
}
MatA{
}
</style>
<script type="text/javascript">
function AddMatrix() {
row=new Array();
cell=new Array();
row_num=document.getElementById("noofrows").value;
cell_num=document.getElementById("noofcols").value;
tab=document.createElement('table');
tab.setAttribute('id','newtable');
tbo=document.createElement('tbody');
for(c=0;c<row_num;c++){
row[c]=document.createElement('tr');
row[c].TagName="tr";
for(k=0;k<cell_num;k++) {
cell[k]=document.createElement('td');
cell[k].TagName="td";
var input = document.createElement("input");
input.type = "text";
input.name = 'node'+c+','+k;
input.setAttribute('class','node');
cell[k].appendChild(input);
row[c].appendChild(cell[k]);
}
tbo.appendChild(row[c]);
}
tab.appendChild(tbo);
document.getElementById('mytable').appendChild(tab);
}
</script>
<script type="text/javascript">
function show1()
{
var tblData = new Array();
var table = document.getElementById("newtable");
var i;
var txt;
var txt2;
tr = table.getElementsByTagName('tr');
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td');
var td2 = td[1].childNodes[0].value;
var td1 = td[0].childNodes[0].value;
var td3 = td[2].childNodes[0].value;
var val = td1+td2+td3;
alert(val);
}
}
</script>
</head>
<body>
<div>
<h3>Matrix</h3>
Number of Rows: <input type="text" id="noofrows" value='2' />
Number of Colomns: <input type="text" id="noofcols" value='3'/>
<input type="button" id="add" value="Add" onclick="AddMatrix()"/>
</div>
<div id="mytable"></div>
<input type="button" id="show" value="Show" onclick="show1()"/>
</body>
</html>
i want to create a pop up in which a form will show,and the field is name,address,city and image,and the image must store in database and i like to edit and update inside the pop up window,will anyone help me regarding tha??????
这篇关于如何从html中动态创建的文本框中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文