使用javascript / jquery将多个输入字段中的值传递给弹出窗口

时间:2013-07-29 05:47:09

标签: javascript jquery html forms

我正在尝试将多个输入字段传递给弹出页面。 这就是我所做的:

<tr>
<th>Item Category</th>
    <td><input type="text" name="object_category" disabled="disabled" id="pid1" />
    </td>
<tr>
<th>Item Name</th>
    <td><input type="text" name="object_name" disabled="disabled" id="pid2" />
    <input type="button" id="item_name" name="choice" onClick="selectValue2('id2')" value="?"></td>
</tr>

通过从其他页面返回其值来填充值。

现在我想使用javascript将id:pid1和id:pid2的值传递给新的弹出页面。 这是我的selectValue2()函数定义:

function selectValue2(pid2){
    // open popup window and pass field id
  var category = getElementById('pid1');
    window.open("search_item.php?id=pid2&&cat="+category+""",'popuppage',
  'width=600,toolbar=1,resizable=0,scrollbars=yes,height=400,top=100,left=100');
}

但是,selectValue2不起作用,因为弹出窗口没有打开。如何将这两个字段的值传递给我的新弹出窗口?

4 个答案:

答案 0 :(得分:1)

这里有问题:

var category = getElementById('pid1');

您需要将其替换为:

var category = document.getElementById('pid1');

由于getElementById适用于document对象。

答案 1 :(得分:0)

您需要使用

document.getElementById

此外,您需要使用值,因为getElementById正在抓取整个元素

您的代码看起来像:

function selectValue2(pid2){
    // open popup window and pass field id
  var category = document.getElementById('pid1').value;
    window.open("search_item.php?id=pid2&&cat=" + category + """,'popuppage',
  'width=600,toolbar=1,resizable=0,scrollbars=yes,height=400,top=100,left=100');
}

您可以为第二个pid值执行类似操作 - 不确定将其传递给函数的原因。

答案 2 :(得分:0)

试试这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function selectValue2(){
    // open popup window and pass field id
  var category = document.getElementById('pid1').value;
  var pid = document.getElementById('pid2').value;
    window.open("test.php?id="+pid+"&cat="+category+"",'popuppage',
  'width=600,toolbar=1,resizable=0,scrollbars=yes,height=400,top=100,left=100');
}
</script>
</head>

<body>
<tr>
<th>Item Category</th>
    <td><input type="text" name="object_category" id="pid1" />
    </td>
<tr>
<th>Item Name</th>
    <td><input type="text" name="object_name"  id="pid2" />
    <input type="button" id="item_name" name="choice" onClick="selectValue2()" value="?"></td>
</tr>
</body>
</html>

答案 3 :(得分:0)

for Jquery,

var pid1Val = $("#pid1").val();
var pid2Val = $("#pid2").val()

for Javascript,

var pid1Val = document.getElementById('pid1');
var pid2Val = document.getElementById('pid2');