将值传递给弹出窗口

时间:2016-05-01 13:24:10

标签: javascript html windows popupwindow

我有一些文本字段用于发送到弹出窗口(我想发送productPrice和productName):

<tr>
  <form>
  <td> ${product.getProductName()}</td>
    <input type="hidden" name="productName" value=${product.getProductName()}>
  <td>${product.getPrice()}</td>
    <input type="hidden" name="productPrice" value=${product.getPrice()}>
  <td>
    <input type="button" onclick="makeOrder()" value="Make order"/>
  </td>
  </form>
</tr>

JS:

 <script type="text/javascript">

function makeOrder(){
  window.open("/user/makeOrder","","height=250,width=400,status=no,location=no,toolbar=no,directories=no,menubar=no");
}

如何将变量ProductName和ProductPrice传递给弹出窗口&#34; / user / makeOrder&#34; ?

1 个答案:

答案 0 :(得分:2)

您可以将ProductNameProductPrice作为查询字符串传递给新的location,然后使用search.split()while循环来设置属性,对象的值。

function makeOrder(name, price){
  window.open("/user/makeOrder?ProductName=" + name + "&ProductPrice=" + price,""
 ,"height=250,width=400,status=no,location=no,toolbar=no,directories=no,menubar=no");
}

新开放的window

var params = location.search.split(/\?|=|&/).filter(Boolean);
var obj = {}, i = 0;
while (i < params.length) {
  obj[params[i]] = params[++i];
  ++i;
}
console.log(obj)