Js无法获取动态创建的text的值
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="attrlayeredit.aspx.cs" Inherits="WebUI.attrlayeredit" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link type="text/css" rel="stylesheet" href="style/prosystem.css" />
<link href="style/userindex.css" type="text/css" rel="stylesheet" />
<link href="style/base.css" type="text/css" rel="stylesheet" />
</head>
<body>
<script type="text/javascript">
var newItemNumber = 0;
function Addattr() {
var Mpara = document.getElementById("ProperName").value;
var Ipara = document.getElementById("ProperValue").value;
if (Mpara == "") {
alert('属性名称不能为空');
return;
}
if (Ipara == "") {
alert('属性值不能为空');
return;
}
var StrConne;
//原因 StrConne无法获取动态的document.getElementById("Stu" + i).value的值
//alert(document.getElementById("Stu" + i).value); 是可以弹出来的
var total = document.getElementById("totalNumber").value;
for (var i = 0; i < total + 1; i++) {
StrConne = document.getElementById("Stu" + i).value;
document.getElementById("totalNumbervalue").value = document.getElementById("Stu" + i).value;
}
alert(document.getElementById("totalNumbervalue").value);
alert(StrConne);
//window.location = "attrlayeredit.aspx?Mpara=" + Mpara + "&Ipara=" + Ipara + "";
}
function additem() {
var row, cell, str;
row = document.getElementById("tb").insertRow();
if (row != null) {
cell = row.insertCell();
cell.innerHTML = "<input type=\"text\" id=\"Stu" + newItemNumber + "\" ><input type=\"button\" value=\"删除\" onclick=\'deleteitem(this);\'>";
document.getElementsByName("totalNumber")[0].value = newItemNumber++;
}
}
function deleteitem(obj) {
var curRow = obj.parentNode.parentNode;
tb.deleteRow(curRow.rowIndex);
document.getElementsByName("totalNumber")[0].value = eval(document.getElementsByName("totalNumber")[0].value) - 1;
}
</script>
<div class="editlayer">
<h4 class="edit-title">修改产品属性</h4>
<div class="edit-list">
<ul>
<li>
<label class="label-before">属性名称:</label>
<input id="ProperName" name="ProperName" type="text" class="blueborder text-long" />
</li>
<li>
<label class="label-before">属性值:</label>
<div class="fl">
<ul class="edit-lists">
<li><input id="ProperValue" name="ProperValue" type="text" class="text" /></li>
</ul>
<a href="javascript:additem()" class="bluetext" >+添加新属性值</a>
<table id="tb"></table>
</div>
</li>
</ul>
<label class="label-before"></label>
<a href="javascript:Addattr()" class="button-like-short mr20">保存</a>
<a href="javascript:void(0)" class="button-like-short close">取消</a>
</div>
</div>
<input type="hidden" name="totalNumber" id="totalNumber"/>
<input type="input" name="totalNumbervalue" id="totalNumbervalue" style="display:none"/>
</body>
</html>
function Addattr() { var Mpara = document.getElementById("ProperName").value; var Ipara = document.getElementById("ProperValue").value; if (Mpara == "") { alert('属性名称不能为空'); return; } if (Ipara == "") { alert('属性值不能为空'); return; } var StrConne = ''; var ipts = document.getElementById('tb').getElementsByTagName('input'); for (var i = 0; i < ipts.length; i++) if (ipts[i].type == 'text') { StrConne += ipts[i].value; } document.getElementById("totalNumbervalue").value = StrConne; //原因 StrConne无法获取动态的document.getElementById("Stu" + i).value的值 //alert(document.getElementById("Stu" + i).value); 是可以弹出来的 /* var total =parseInt( document.getElementById("totalNumber").value)///////要转为数字,value属性为字符串,要不可能会按照字符串操作,以后注意一定要转换类型 for (var i = 0; i < total + 1; i++) { StrConne = document.getElementById("Stu" + i).value; document.getElementById("totalNumbervalue").value = document.getElementById("Stu" + i).value; }*/ alert(document.getElementById("totalNumbervalue").value); alert(StrConne); //window.location = "attrlayeredit.aspx?Mpara=" + Mpara + "&Ipara=" + Ipara + ""; }
逻辑也有问题,你增加那么多属性,但是循环的效果就是给 document.getElementById("totalNumbervalue").value赋最后一个document.getElementById("Stu" + i).value的值?。。其实我的最后的答案。就是
for (var i = 0; i < ipts.length; i++)
if (ipts[i].type == 'text') {
StrConne += ipts[i].value+“,”;
}
问题已经解决