elliott.david 2015-04-17 17:58 采纳率: 25%
浏览 8

按钮没有到达ajax

I am really puzzled as why the 2nd approach is not getting to ajax:

1. (This is firing to ajax ok)

<div id="div_comment">
    <textarea name="text_comment" id="text_comment" placeholder="160 chars or less, no # or @" data-role="none" rows=11 cols=20 onkeypress="if(event.keyCode==13){return false;}" onKeyDown="limitText2(this,160);" onKeyUp="limitText2(this,160);" style="resize:none;"></textarea>
    <button id="comment_btn" class="ui-btn ui-btn-inline" style="border: #ffffff 1px solid;">Enter your comment</button>
</div> <!-- div_comment -->

2. (This is not firing to ajax)

document.getElementById('div_comment').innerHTML = "<br> <br> <textarea name=\"text_comment\" id=\"text_comment\" placeholder=\"160 chars or less, no # or @\" data-role=\"none\" rows=11 cols=20 onkeypress=\"if(event.keyCode==13){return false;}\" onKeyDown=\"limitText2(this,160);\" onKeyUp=\"limitText2(this,160);\" style=\"resize:none;\"></textarea> <span id=\"commenterr\" style=\"font-weight:bold; font-size:90%;\"></span> <br style=\"line-height:190%;\"> <button id=\"comment_btn\" class=\"ui-btn ui-btn-inline\" style=\"border: #ffffff 1px solid;\">Enter your comment</button> </center> <a href=\"#searchpage\" data-transition=\"slide\"> Back </a>";

Both using the same ajax:

$(document).ready(function(){
  $("#comment_btn").click(function(){ //click write_comment a tag btn
  console.log ("ready-comment_btn: clicked comment_btn");
    $.post("cgi-bin/s_comment.pl", {
        comment:"HELLO WORLD IN READY",
    },
    function(data,status){
      document.getElementById('div_comment').innerHTML = data;
    });
  }); //write_comment
}); //ready

The second approach has the same content assigned to innerHTML, but not working, when click on the button, no response, just an orange border glow.

Is this because of Chrome ?

  • 写回答

2条回答 默认 最新

  • Memor.の 2015-04-17 18:01
    关注

    change this to use .on, that way all dynamic elements will be taken care of.

    $(document).on("click","#comment_btn",function(){ //click write_comment a tag btn
      console.log ("ready-comment_btn: clicked comment_btn");
        $.post("cgi-bin/s_comment.pl", {
            comment:"HELLO WORLD IN READY",
        },
        function(data,status){
          document.getElementById('div_comment').innerHTML = data;
        });
      }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程