简体   繁体   English

未注册 JQuery 单击事件<ul>从 PHP 脚本返回

[英]JQuery click event is not being registered on <ul> returned from PHP Script

I have a HTML page which contains 2 div s.我有一个包含 2 个div的 HTML 页面。 In its first div a ul is populated immediately after page loading using AJAX from a PHP script.在它的第一个div使用 AJAX 从 PHP 脚本加载页面后立即填充ul And 2nd div there is a static ul .第二个div有一个静态ul Now I want that in both div s on every ul a click event should work.现在我希望在每个ul上的两个div ,单击事件都应该起作用。 Ie, an alert should come saying that it's clicked.即,应该出现一个警报,说它被点击了。

Here is the PHP code:这是PHP代码:

require_once "../../resources/script/dbconfig.php";

$query = "SELECT * FROM job_case";
$rslt = mysqli_query($link,$query);     

echo "<ul>";

for($i=0;$i<mysqli_num_rows($rslt);$i++)
{
$job = mysqli_fetch_row($rslt);
echo "<li class=\"jobTitle\" id=jon".$job[0].">".$job[1]."</li>";           
}

echo "</ul>";

HTML Page: HTML页面:

<div class="jobCase" id="govtJobCase">
    <ul>
        <li class="jobTitle" id="jobId">Item A</li>
        <li class="jobTitle" id="jobId">Item B</li>         
    </ul>
</div>        
<div class="jobCase">
    <ul>
         <li class="jobTitle" id="jobId">Item A</li>
         <li class="jobTitle" id="jobId">Item B</li>
     </ul>        
 </div>

My problem is it that in first div published from PHP, there is no event working but in other static ul tag event is working properly.我的问题是,在从 PHP 发布的第一个div ,没有事件工作,但在其他静态ul标记事件中工作正常。

you should provide us with your jQuery script.您应该向我们提供您的 jQuery 脚本。 But I can help you maybe:但我可以帮助你也许:

for the event, you should not use on() and rather better to stick to delegate() since it allows event attachment to the DOM objects which are yet to be created after the initial page load.对于事件,您不应该使用on()而最好坚持使用delegate()因为它允许将事件附加到初始页面加载后尚未创建的 DOM 对象。 For instance if your ajax requests returns a <uL class='sample'></ul> you can then do:例如,如果您的 ajax 请求返回<uL class='sample'></ul>您可以执行以下操作:

$(document).delegate(".sample", "click", function(){
alert("You have clicked on a newly created <ul>");
});

If you put the event handler on an element around both divs you can update the contents of the element all you want but the event handler stays in tact.如果您将事件处理程序放在两个 div 周围的元素上,您可以随心所欲地更新元素的内容,但事件处理程序保持原样。

<div class="container">
    <!-- the contents of this div is populated using AJAX -->
</div>

javascript: javascript:

$('.container').on('click', function () {
    // clicks will arrive here even when they originate from ajax loaded content
});

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

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