繁体   English   中英

在模式对话框窗口中选择打开器页面元素?

[英]Selecting opener page element within from the modal dialog window?

//Html - page containing link to open dialog

<input type="text" value="something" id="inputbox_id" />
<a href="#" class="opendialog">Open</a>

//Javascript

.opendialog.click function 
{
$('.modaldialog').click(function(event) {
    $('<div id="dialogContainer"> </div>').appendTo('body');

   //ajax call to page.php, if successful then add output of page.php to 
   //dialogContainer div created above
   //page.php has a button and some javascript as below
}


//Html - content of page.php
<input type="button" id="button" value="I am button" />

//Javascript on page.php
// On click "#button" 
// $('#inputbox_id').val("Something New");

但它没有用,而是出现错误“未定义inputbox_id”...。

所以我将代码更改为

$('#input_box_id', 'body').val(); // didn't work

$('body').find('#input_box_id').val("some value"); //Worked

我的问题是-

为什么$(selector,context)选择器在这种情况下不起作用? 使用select body然后找到所需元素可以吗? 您能提出更好的建议吗?

单击#button后如何关闭此对话框?

我感谢您的帮助!

更新

对话框关闭问题已解决-只需调用$(“#IdOfDialogContainer”)。remove();

$('#input_box_id', 'body').val(); 

这种用法是错误的,您必须在第二个参数中提供DOM元素。 像这样:

$('#input_box_id', document.getElementsByTagName('body')).val(); 

另一种方法是-您在帖子中提到的内容,

$(body).find('#input_box_id').val(); 

这已经在JQuery官方网页中提到:

在内部,选择器上下文是通过.find()方法实现的,因此$('span',this)等效于$(this).find('span')。*

资料来源: http//api.jquery.com/jQuery/

因此,您无需按照我所说的第一种方式实施,就不会出现性能问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM