weixin_33720956 2015-02-04 14:24 采纳率: 0%
浏览 8

jQuery在ajax内容中[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/203198/event-binding-on-dynamically-created-elements" dir="ltr">Event binding on dynamically created elements?</a>
                            <span class="question-originals-answer-count">
                                (23 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2015-02-04 14:26:31Z" class="relativetime">5 years ago</span>.</div>
        </div>
    </aside>

I have this page:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>test</title>
  <script src="jquery-2.1.1.min.js"></script>
  <script language="javascript" src="ajax.js"></script>
  <script>
    $(document).ready(function() {
      $('#input').keyup(function() {
        $('#output').html($(this).val().replace(/
/g,'<br/>')); 
      });
    });
  </script>
</head>

<?php
ini_set("display_errors","On"); 
    echo "<div id=\"UpdateArea\">";
        echo "<input type=\"button\" value=\"type here\" onClick=\"ShowPage()\">";
    echo "</div>";
}

?>
</html>

And when I push the button it loads this into the UpdateArea Div:

<?php

echo "<textarea id=\"input\" rows=\"10\" cols=\"50\" class=\"textarea\"></textarea><br><br><br>";

echo "<div id=\"output\"></div>";

?>

But the jQuery wont Work on the Ajax loaded contents :/ So the input won't show up ind the output div.. Why is that?, I'm new to jQuery, so it might be a obvious problem. I would really appreciate some help :)

The Jquery Works fine if I include the script a jQuery on a page for it self and load it.

Kind regards Juel

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33728708 2015-02-04 14:25
    关注

    You need to use event delegation for attaching events to dynamically added elements:

    $(document).on('keyup','#input',function() {
       $('#output').html($(this).val().replace(/
    /g,'<br/>')); 
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题