尝试将html表单中的变量值添加到javascript中

Trying to add variable value from an html form into javascript

本文关键字:变量值 添加 javascript html 表单      更新时间:2023-09-26

我在下面设置了一个表单,它有两个输入Keywords和最小价格

<form name="Data" method="GET" action="#">
<table cellpadding="2" border="0">
  <tr>
    <th>Keywords</th>
    <th>Min Price</th>
  </tr>
  <tr>
    <td><input type="text" name="keywords" id="keywords"/></td>
    <td><input type="text" name="MinPrice" id="MinPrice"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><INPUT type="submit" name="submit" value="Search">
    </td>
  </tr>
</table>
</form>

然后,获得的结果需要进入一个javascript,然后该javascript应该返回基于变量的结果。JS在没有变量的情况下工作,即只将它们放在代码中,但我不知道如何引用上面的表单,然后将代码作为一个完整的提交

      html.push('</tbody></table>');
  document.getElementById("results").innerHTML = html.join("");
}  
var filterarray = [
  {"name":"MaxPrice",
   "value":"1000",
    "name":"MinPrice",
   "value":Variable here!!,
   "paramName":"Currency",
   "paramValue":"GBP"},

上面是脚本的一部分,其中一个值将转到

任何帮助apreciated p.s.完全新手,所以慢慢来

您的选项列表不正确,多个属性的名称相同。

这里有一种不同的呈现方式:

var maxPrice = document.getElementById('MaxPrice').value;
var minPrice = document.getElementById('MinPrice').value;
var filterOptions = {
    "MaxPrice": maxPrice,
    "MinPrice": minPrice,
    "paramName":"Currency",
    "paramValue":"GBP"
};

然后可以在代码中使用它们。例如,最低价格为filterOptions.MinPrice

要从MinPrice输入文本中检索内容,只需执行以下操作:

var minPrice = document.getElementById('MinPrice').value;

或者更简单地使用jquery:

var minPrice = $('#MinPrice').val();

至于你建立阵列的方式,对我来说没有多大意义:

var filterarray = [
  {"name":"MaxPrice",
   "value":"1000",
    "name":"MinPrice",
   "value":Variable here!!,
   "paramName":"Currency",
   "paramValue":"GBP"},
...

在这里,您正在构建一个多次包含相同字段(名称和值)的对象数组。这对我来说没有多大意义。你可能应该重新考虑你试图填充数组的文字对象的结构。