gpt4 book ai didi

javascript url 值到多个字段

转载 作者:行者123 更新时间:2023-12-02 17:43:30 26 4
gpt4 key购买 nike

我正在构建一个表单,除了最后部分之外,一切都很顺利。我试图将 URL 中的值传递到多个字段中,我可以将其放入第一个字段中,但不能放入其他字段中。

这里是表单中的代码,希望足以让您看到。

<script type="text/javascript" language="javascript">
function populate() { //get the url variables
var varSection = window.location.search.substr(1);
var varArray = varSection.split("&")
for(var v=0; v<varArray.length; v++) {
var keyValueArray = varArray[v].split("="); //check the pair
}
if(keyValueArray[0]=="id") {
varValue=keyValueArray[1];
document.getElementById("QuoteID1[]").value=varValue;
}

}

</script>
</head>

<body style="font-family:Arial, Helvetica, sans-serif" onLoad="populate()">

<form name="MakeQuote" method="post" action="QuoteProcess.php">
<table width="100%" border="0">
<tr valign="top">
<td height="34"><table width="960" border="0">
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td width="40%" rowspan="4" valign="top"><table width="100%" border="0">
<tr>
<td width="49%">&nbsp;</td>
<td width="11%">Weight</td>
<td width="40%" class="right">QuoteID</td>
</tr>
<tr>
<td align="right">Sand</td>
<td><input name="Weight[]" type="number" id="Weight[]" size="8" maxlength="4" value="0.00"></td>
<td>Kg
<input type="hidden" name="ProductID[]" id="ProductID[]" value="2">
<input name="QuoteID1[]" type="number" size="8" maxlength="8" id="QuoteID1[]" value=""></td>
</tr>
<tr>
<td align="right">Shingle</td>
<td><input name="Weight[]" type="number" id="Weight[]" size="8" maxlength="4" value="0.00"></td>
<td>Kg
<input type="hidden" name="ProductID[]" id="ProductID[]" value="3">
<input name="QuoteID1[]" type="number" size="8" maxlength="8" id="QuoteID1[]" value="" >
</td>
</tr>
<tr>
<td align="right">Topsoil</td>
<td><input name="Weight[]" type="number" id="Weight[]" size="8" maxlength="4" value="0.00"></td>
<td>Kg
<input type="hidden" name="ProductID[]" id="ProductID[]" value="4">
<input name="QuoteID1[]" type="number" size="8" maxlength="8" id="QuoteID1[]">
</td>
</tr>
<tr>
<td align="right">Mulch</td>
<td><input name="Weight[]" type="number" id="Weight[]" size="8" maxlength="4" value="0.00"></td>
<td>Kg
<input type="hidden" name="ProductID[]" id="ProductID[]" value="4">
<input name="QuoteID1[]" type="number" size="8" maxlength="8" id="QuoteID1[]">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>

我们将不胜感激任何帮助。

最佳答案

那是因为您有多个具有相同 ID 的元素。 ID 应该唯一getElementById 仅选择具有该 ID 的第一个元素。

您可以select the elements by their name并迭代它们以设置它们的值:

var inputs = document.getElementsByName('QuoteID1[]');

for (var i = 0, l = inputs.length; i < l; i++) {
inputs[i].value = varValue;
}
<小时/>

仅供引用,您不必将 [] 添加到 ID,它们仅与元素的 name 相关,并且仅 if you are using PHP on the server side .

关于javascript url 值到多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978083/

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