简体   繁体   English

单击时 jQuery 无法识别 PHP 生成的子元素

[英]jQuery does not recognize PHP generated child element when clicked

I am working on a website on which I have to display buttons based on database content.我正在一个网站上工作,我必须在该网站上根据数据库内容显示按钮。 Clicking these buttons should then send a request to the database and reload the content of the page appropriately.单击这些按钮应向数据库发送请求并适当地重新加载页面内容。

Everything is done with Ajax and jQuery by executing PHP scripts.一切都是通过执行 PHP 脚本使用 Ajax 和 jQuery 完成的。 I can generate the buttons, but jQuery does not trigger when they are clicked.我可以生成按钮,但是单击它们时 jQuery 不会触发。

It seems that jQuery detects a click on the div containing the buttons but not on the buttons themselves.似乎 jQuery 检测到对包含按钮的 div 的点击,而不是在按钮本身上的点击。

I was told that it may be because the jQuery script is loaded before the page is updated with the new HTML content.有人告诉我,这可能是因为在使用新的 HTML 内容更新页面之前加载了 jQuery 脚本。

This is the div container for all the choice buttons:这是所有选择按钮的 div 容器:

<div id = "choices">
     <!-- Choices button will be displayed here -->
</div>

Here's the PHP code creating the HTML for the buttons:这是为按钮创建 HTML 的 PHP 代码:

echo " <button class = 'choice' id = \"$id\">
        $content
      </button> ";

This code is loaded in the previous #choices div by this jQuery code :此代码通过此 jQuery 代码加载到先前的 #choices div 中:

$.post("php_script/getChoices.php", 

       function(result){
        $("#choices").html(result);

       });

Pretty basic, and the expected (and actual) output on the webpage is :非常基本,网页上的预期(和实际)输出是:

<div id = "choices">

     <button class = 'choice' id = "1">
        Yes.
     </button>

     <button class = 'choice' id = "2">
        No.
     </button>

</div>

But when I write this :但是当我写这个时:

$(".choice").click(function());

It never triggers, no matter how basic the function.无论功能多么基本,它都不会触发。 However, having another event to interact with the buttons is possible.然而,有另一个事件与按钮交互是可能的。 For example, the code below does hide the buttons when the #choices div is clicked.例如,当点击#choices div 时,下面的代码确实隐藏了按钮。

$("#choices").click(function(){
      $(".choice").hide(); 
});

In order to understand the problem, I wrote this jQuery script that print the content of the element that was clicked on the console.为了理解这个问题,我写了这个 jQuery 脚本来打印在控制台上点击的元素的内容。

$("*").on('click', function(event){

    event.stopPropagation();
    console.log('Clicked: ' + $(this).html());

});

And when I click on a button, it always returns the whole #choices div instead of just the content of the button.当我点击一个按钮时,它总是返回整个#choices div 而不仅仅是按钮的内容。

As I said, I think this is because I am trying to interact with HTML elements that were added after the jQuery script was loaded (it is written in the section of my page).正如我所说,我认为这是因为我试图与加载 jQuery 脚本后添加的 HTML 元素进行交互(它写在我页面的部分中)。

Is my assumption correct and what should I do in order to trigger an action when the button themselves are clicked ?我的假设是否正确,我应该怎么做才能在单击按钮本身时触发操作?

You can always target the document to trigger dynamically created elements:您始终可以将文档定位为触发动态创建的元素:

$(document).on('click', '.choice', function (e) {
    e.preventDefault(); //stop default behaviour
    var id = this.id; //get element ID
    $(this).hide(); //hide element
});

As mentioned you can use event delegation如前所述,您可以使用事件委托

$(function(){
    $("#choices").on("click", "button", function(){
        alert("clicked");
    });
});

Or manually bind the event handler when you add the new buttons或者在添加新按钮时手动绑定事件处理程序

// called each time new buttons are added
function bindOnClick(){
    $("#choices").off();
    $("#choices").on("click", "button", function(){
        alert("clicked");
    });
}

$(function(){
     // called once when dom is ready
     bindOnClick();
});

If you don't call off you will bind the onclick event to the same element more than once.如果您不取消,您将不止一次将onclick事件绑定到同一个元素。

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

相关问题 jQuery:单击父元素时,隐藏子元素。 单击子元素时,不要隐藏它 - jQuery: When parent element is clicked, hide child element. When child element is clicked, don't hide it 单击父元素时,jQuery在子元素中淡入淡出 - jQuery Fading in a Child Element When Parent Element is Clicked jQuery在动态生成内容时获取被点击元素的属性 - jQuery get attribute of clicked element when content is dynamically generated Jquery:单击模态确认按钮时删除动态生成的元素 - Jquery: Removing dynamically generated element when modal confirmation button is clicked jQuery识别单击的子元素 - jQuery identify clicked child element jQuery选择器:未单击某些子项时将规则应用于元素 - JQuery selectors: Apply rule to element when certain child is NOT clicked 在JQuery中单击父元素时如何定位每个子元素 - How to target each child element when their parent is clicked in JQuery 仅在单击父div中的子元素时执行的jQuery - jQuery that executes only when a child element in the parent div is clicked 单击元素时,jQuery不会使用active类更改元素 - JQuery does not change element with `active` class when element is clicked 如何检测单击的元素是否不是jQuery中元素的子级? - How to detect if clicked element was not a child of an element in jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM