javascript setAttribute("name",newname)不会更改文本框的名称,它会创建SubmitName [英] javascript setAttribute("name",newname) not change textbox's name it create submitName
问题描述
我使用此功能在表中创建新行
i use this function to create a new row in table
function addRow(obj)
{
var table = document.getElementById("table2");
var rowCount = table.rows.length-1;
var row = table.insertRow(rowCount);
var nowrownum = table.rows.length-1;
var colCount = table.rows[2].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[2].cells[i].innerHTML;
newcell.style.cssText = table.rows[2].cells[i].style.cssText;
//Here is problem
newcell.childNodes[0].setAttribute("name",table.rows[2].cells[i].childNodes[0].getAttribute("name")+nowrownum);
//End here
newcell.childNodes[0].id = table.rows[2].cells[i].childNodes[0].id+nowrownum;
switch(newcell.childNodes[0].type)
{
case "text":
newcell.childNodes[0].value = "";
break;
}
}
obj.style.visibility = "hidden"; //to hide current button
}
这是我的html代码
<form name=form id=form method=POST target="frametemp">
<table name ="table2" id="table2" border="1" align="Center">
<tr>
<th>Head1</th>
<th>Head2</th>
<th>Head3</th>
<th>Head4</th>
</tr>
<tr>
<td>xxxxx</td>
<td><input type="text" id="edit_0" name="edit_0" ></td>
<td>yyyy</td>
<td><input id="add_bt_0" onclick="JavaScript : addRow(this);" name="add_bt_0" value="addrow" type="button" ></td>
</tr>
在添加行之后,我从开发人员工具检查页面
after row is add i check the page from ie developer tool
<td><input type="text" id="edit_1" name="edit_0" submitName="edit_1" ></td>
<td><input id="add_bt_1" onclick="JavaScript : addRow(this);" name="add_bt_0" submitName="add_bt_1" value="addrow" type="button" ></td>
name属性不会更改,但会创建SubmitName.
the name attribute does not change but it create submitName.
如何更改名称属性.我的目标浏览器是IE 7 ++(现在我使用的是9)
how can i make name attribute change. my target brower is IE 7++(now i use ie 9)
推荐答案
他们在IE8中解决了此问题.在以前的版本中,调用createElement时需要包括名称.从MSDN:
They fixed this in IE8. In previous versions, you need to include the name when you call createElement. From MSDN:
Internet Explorer 8和更高版本可以在运行时设置NAME属性关于使用IHTMLDocument2 :: createElement动态创建的元素方法.在早期版本中创建具有NAME属性的元素Internet Explorer的名称,请在使用时包括属性及其值IHTMLDocument2 :: createElement方法.
Internet Explorer 8 and later can set the NAME attribute at run time on elements dynamically created with the IHTMLDocument2::createElement method. To create an element with a NAME attribute in earlier versions of Internet Explorer, include the attribute and its value when using the IHTMLDocument2::createElement method.
这是来自MSDN的示例:
Here is the example from MSDN:
var newRadioButton = document.createElement("<INPUT TYPE='RADIO' NAME='RADIOTEST'VALUE='First Choice'>")
这篇关于javascript setAttribute("name",newname)不会更改文本框的名称,它会创建SubmitName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!