gpt4 book ai didi

javascript - 从模式对话框窗口中选择开启页面元素?

转载 作者:行者123 更新时间:2023-11-28 09:50:29 25 4
gpt4 key购买 nike

//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官方网页已经提到了:

Internally, selector context is implemented with the .find() method, *so $('span', this) is equivalent to $(this).find('span').*

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

所以,你不需要按照我说的第一种方式来实现,不会有性能问题。

关于javascript - 从模式对话框窗口中选择开启页面元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11124473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com