简体   繁体   English

访问由AJAX调用创建的DOM元素

[英]Access DOM elements created by AJAX call

I have an ajax call that returns a bunch of html, including some hidden divs with values in as below: 我有一个ajax调用,它返回一堆html,包括一些隐藏的div,其值如下:

<span class="hidden" name="price1h1" id="price1h1c0a1adult">90.7375</span>
<span class="hidden" name="price1h1" id="price1h1c0a1child">90.7375</span>
<span class="hidden" name="price1h1" id="price1h1c0adultPreMargin">265</span>
<span class="hidden" name="price1h1" id="price1h1c0childPreMargin">247</span>
<span class="hidden" name="price1h1" id="price1h1c0adultPostMargin">65</span>
<span class="hidden" name="price1h1" id="price1h1c0childPostMargin">0</span>
<span class="hidden" name="price1h1" id="price1h1c0margin">35</span>
<span class="hidden" name="price1h1" id="price1h1c0partyAdults">2</span>
<span class="hidden" name="price1h1" id="price1h1c0partyChildren">2</span>

In my ajax call's success function, i add this html to a div, and then try to call a js function which on clicking of some radio buttons performs some calculations and generates a final price using the above span elements values 在我的ajax调用的成功函数中,我将此html添加到div中,然后尝试调用js函数,该函数在单击某些单选按钮时执行一些计算,并使用上述span元素值生成最终价格

The visual stuff all appears, so the call executes, but when the js function tries to access the result using: 视觉内容全部出现,因此调用将执行,但是当js函数尝试使用以下方法访问结果时:

var partyAdults = $('span#price1h1c0partyAdults').html();
alert(partyAdults);

the value is undefined 值未定义

This all works fine when it is inline in the page, but how can i access the contents of what I have generated? 当页面内联时,所有这些都可以正常工作,但是如何访问生成的内容?

Thanks 谢谢

如果需要动态访问DOM元素,则可以指定在元素准备就绪时将调用的处理程序: http : //api.jquery.com/ready/

Try the following code: 尝试以下代码:

var partyAdults = $('span#price1h2c0a1adult').live().text();

You can use JQuery .live() function to access the dynamically created elements. 您可以使用JQuery .live()函数访问动态创建的元素。

Demo 演示版

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

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