简体   繁体   English

在动态生成的情况下调用jQuery模式

[英]call jquery modal in dynamically generated <span>

I'm trying to open a modal pop-up using dynamically generated content, so far it's not working well. 我正在尝试使用动态生成的内容打开模式弹出窗口,到目前为止,它不能很好地工作。 My php code generates a set of elements with the title of the events, and when clicked on the title, I need to open a popup, again it's content is dynamic. 我的php代码生成带有事件标题的一组元素,单击标题时,我需要打开一个弹出窗口,它的内容还是动态的。

Following is my code: 以下是我的代码:

<?php
echo '<span id="rentalRates" name="rentalRates" ><u style="cursor:pointer;"><h2>'.$row["header"].'</h2></u></b></span>
<div style="width:550px;overflow-y:auto;"  class="details"  id="divAuditoriumGuidelines">
<div id="divRentalRates'.$x.'"  title="Rental Rates" name = "divRentalRates'.$x.'">
<p>'.$row["longDescription"].'</p> </div>';
echo '</div>
<div  class="closer"></div>
</div>
</li>';
$x = $x + 1;
?>

Following is my jQuery: 以下是我的jQuery:

$("#divRentalRates0").dialog(
{
    autoOpen: true,
    width: 750,
    height: 650,
    show:
    {
        effect: "blin",
        duration: 1000
    },
    hide:
    {
        effect: "explode",
        duration: 1000
    }
});

$("#rentalRates1").click(function()
{
    $("#divRentalRates0").dialog("open");
});

$("#rentalRates2").click(function()
{
    $("#divRentalRates0").dialog("open");
});

However, this doesn't work. 但是,这不起作用。 But if I put a static modal popup outside the looping, it works fine. 但是,如果我在循环外放置一个静态模式弹出窗口,则可以正常工作。 Can someone help me with this please? 有人可以帮我吗?

$(document).on('click','#rentalRates1,#rentalRates1',function()
{
    $("#divRentalRates0").dialog("open");
});

The above code will work, but to be more specific, 上面的代码可以使用,但更具体地说,

Use this Solution : 使用此解决方案

$(document).on('click','[id^="rentalRates"]',function()
{
    $("#divRentalRates0").dialog("open");
});

Use event delegation - on() 使用事件委托-on ()

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

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