javascript url 值到多个字段

标签 javascript html dom

我正在构建一个表单,除了最后部分之外,一切都很顺利。我试图将 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/

相关文章:

javascript - 惊人的 CSS 动画

javascript - Selenium:如何检查匿名函数的代码

html - 使用第 n 个 child 时出错

android - 使用适用于 Android 的 JavaMail API 发送 HTML 电子邮件

javascript - d3 操作 html 的最佳方式是什么?

javascript - 提前输入自动完成抛出错误

javascript - 在Vue JS中获取数据属性作为对象: returning [object object] even with JSON. stringify

javascript - 当用户单击第 1 页上的按钮时,它会将数据发送到第 2 页并重定向到第 3 页

html - 如何强制浏览器 DOMDocument.ExecCommand() 函数为默认字体注入(inject) <font> 标签

php - 使用 dom 从 HTML 中删除所有内联样式标签