Ajax元素看不到JQuery

Ajax elements dont see JQuery

本文关键字:JQuery 看不到 元素 Ajax      更新时间:2023-09-26

在我的php页面中,当我用JQuery Ajax按下按钮时,我会附加新按钮和信息,但当我单击Ajax附带的新按钮时,它的JQuery不起作用,但旧按钮的JQuery起作用,我的意思是新按钮看不到我的php页的javascript文件。

我的JQuery Ajax代码;

$("button[name='addnewelement']").click(function() {


$.ajax({    

    type: "POST",
    url: "addelement.php",
    cache: false,
    success: function(html){

    $("#new").append(html);// add new element into index.php
  });
  });

addelement.php;

  echo "
      .......
     <button id="<?php echo $row['id']; ?>" name="addnewelement"  >Add</button>
     ....... 
   " ;

我该如何解决这个问题?

谢谢。

addelement.php页面中的echo类似

echo '<button id="'.$row['id'].'" name="addnewelement"  >Add</button>';
exit;

最好把exit放在echo之后。确保遵循语法并使用良好的IDE来查找语法错误。

您应该使用on click事件处理程序,而不是click事件处理。
以下是链接:http://api.jquery.com/on/
例如

$( document).on( "click", "button[name='addnewelement']", function() {
$.ajax({    
    type: "POST",
    url: "addelement.php",
    cache: false,
    success: function(html){
    $("#new").append(html);// add new element into index.php
  });
});