我有一些文本字段用于发送到弹出窗口(我想发送产品价格和产品名称):
<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 传递到弹出窗口“/user/makeOrder”?
最佳答案
您可以将 ProductName
和 ProductPrice
作为查询字符串传递到新的 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");
}
在新打开的窗口
var params = location.search.split(/\?|=|&/).filter(Boolean);
var obj = {}, i = 0;
while (i < params.length) {
obj[params[i]] = params[++i];
++i;
}
console.log(obj)
关于javascript - 将值传递给弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36967171/