jquery - window.opener 未将值返回到右侧文本框

标签 jquery parent-child window.opener

HTML 代码:

<tr class="item-row">
   <td>
     <input type="button" class='del' value="" style="z-index:16; background-image:url(../images/delete.png); background-repeat:no-repeat; background-position:left center; text-align:right;"/>
   </td>
   <td>
     <input type="button" name="pilihItem[]" value="i" id="pilihItem" class="pilihItem">
   </td>
   <td>
     <input type="text" id="nama_item" name="nama_item[]" value="" style="width: 200px;">
   </td>
</tr>
<tr class="item-row">
   <td>
     <input type="button" class='del' value="" style="z-index:16; background-image:url(../images/delete.png); background-repeat:no-repeat; background-position:left center; text-align:right;"/>
   </td>
   <td>
     <input type="button" name="pilihItem[]" value="i" id="pilihItem" class="pilihItem">
   </td>
   <td>
     <input type="text" id="nama_item" name="nama_item[]" value="" style="width: 200px;">
   </td>
</tr>

打开子窗口的jquery代码:

$("#pilihItem").click(function(){
        var id = 1;
        var name = $(this).prev().attr("name");           
        var url = "../formItem.php?id=" + id + "&name=" + name;

        window.open(url, 'Pilih Item', 'location=1,status=1,scrollbars=1,resizable=no,width=800,height=300'); 
    });

为父窗口设置值的 jquery 代码:

window.opener.$("#nama_item").val($.trim(nama_item));

enter image description here

问题: 我无法设置单击 #pilihItem 按钮的 #nama_item 文本框的值。 它总是将文本框设置在第一行。任何帮助都感激不尽。

最佳答案

您正在使用 ID 来选择输入,而您应该使用唯一的东西。

您需要将 ID 更改为唯一(无论如何它们在 DOM 中应该是唯一的),或者将复选框的名称更改为...

<input type="text" name="nama_item[1]" value="" style="width: 200px;">

并在弹出窗口中更改您的 JS 以使用此名称而不是 ID...

window.opener.$("input[name='nama_item[1]']").val($.trim(nama_item));

关于jquery - window.opener 未将值返回到右侧文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15386817/

相关文章:

Perl:确定函数的定义位置

css - 在 CSS/LESS 中选择 child 的 child ?

Javascript window.opener 在 IE 10 中不起作用

java - 在 play 2 框架中向 json 对象添加下拉列表项

php - ajax删除行的时候如何删除

c# - 当页面长时间打开时,JQuery ajax 调用中断

javascript - 使用 window.opener 检查父窗口在 IE 中不起作用

jquery - 如何防止使用 jQuery 重新打开新窗口?

python - 何时在 PySide 中设置父级

javascript - IE 11 中不同 'windows' 之间的跨域消息传递