gpt4 book ai didi

javascript - 如何将 javascript 变量值存储到 html 输入值中?

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:18 25 4
gpt4 key购买 nike

这是我的 html 代码,在这个简单的代码中,通过按 + 按钮动态地增加输入的数量。现在我想在添加新输入后将 allRows.length+1 值存储到 myHiddenField 中,最后我可以看到我的 inouts html 输入值的总数,如下所示:

<input type="hidden" name="myHiddenField" value="**I want to store allRows.length+1 value here **" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
function addRow(r){
var root = r.parentNode;//the root
var allRows = root.getElementsByTagName('tr');//the rows' collection
var cRow = allRows[0].cloneNode(true)//the clone of the 1st row
var cInp = cRow.getElementsByTagName('input');//the inputs' collection of the 1st row
for(var i=0;i<cInp.length;i++){//changes the inputs' names (indexes the names)
cInp[i].setAttribute('name',cInp[i].getAttribute('name')+'_'+(allRows.length+1))
}
root.appendChild(cRow);//appends the cloned row as a new row
}

</script>
</head>
<body>
<form action="" method="get">
<table width="766" border="0" cellspacing="0" cellpadding="0">
<input type="hidden" name="myHiddenField" value="**I want to store allRows.length+1 value here **" />
<tr>
<td width="191"><input type="text" name="textfield_A" /></td>

<td width="191"><input type="text" name="textfield_B" /></td>

<td width="286"><input name="button" type="button" value="+" onclick="addRow(this.parentNode.parentNode)"></td>
</tr>
</table><br /><br />
<input name="" type="submit" value="Submit" />
</form>
</body>
</html>

如何解决这个问题并通过我的 html 表单将 javascript 值存储到输入值中?

最佳答案

在隐藏输入和 Javascript 中添加一个 id="myHiddenField" 属性,您可以

document.getElementById("myHiddenField").value = allRows.length+1;

您显然不需要 jQuery 来为输入赋值。

关于javascript - 如何将 javascript 变量值存储到 html 输入值中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18998392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com