我有几个动态创建的div,里面有相同的元素:
<div id="(dinamic_id_1)">
<div class="(element_1)">Some text here</div>
<input class="(element_2)" />
<iframe class="(element_3)" src="form.php?id=(dinamic_id_1)" />
</div>
<div id="(dinamic_id_2)">
<div class="(element_1)">Some text here</div>
<input class="(element_2)" />
<iframe class="(element_3)" src="form.php?id=(dinamic_id_2)" />
</div>
...在iframe中有一个表单,它看起来像这样:
<form id="(dinamic_id)" >
<input class="(class_input)" />
</form>和我的jquery函数:
$(".class_input").change(function() {
$(this).closest("form").submit();
});我还想在父窗口的submit上对div中与表单共享相同id的元素执行其他操作。我得到了父div中的html,如下所示:
var parent_div = $("div#"+$(this).closest("form").attr("id"), parent.document);
alert($(parent_div).html());但是我不能访问div中的元素,例如:
var parent_div = $("div#"+$(this).closest("form").attr("id"), parent.document);
alert($(parent_div+" .element_1").html());或
var parent_div = $("div#"+$(this).closest("form").attr("id")+" .element_1", parent.document);
alert($(parent_div).html());返回null而不是"Some text here“。
发布于 2012-02-05 22:46:08
尝试将代码修改为以下内容:
var parent_div = $("div#"+$(this).closest("form").attr("id"), parent.document);
alert(parent_div.children('.element_1').text());我做了一个快速测试,结果一切都很好。
编辑:解释
问题是您试图将parent_div作为字符串引用,而实际上它本身是一个jQuery对象。
alert($(parent_div + " .element_1").html());
// ( [object] + [string] )https://stackoverflow.com/questions/9149228
复制相似问题