【问题标题】:How do you dynamically create buttons and add click handler JQUERY in Django template如何在 Django 模板中动态创建按钮并添加点击处理程序 JQUERY
【发布时间】:2013-06-21 03:57:12
【问题描述】:

我发现了同样的问题,here

我正在尝试动态创建一个按钮,而不是可以在 Django 模板中使用 alert('...') 。 但是,由于某种原因,我的代码无法正常工作。

这是我的全部示例。

 {% block scripts %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>

<script type="text/javascript">
    $(function()
    {

    $('button').addClass('removeButton');

    $("#addRowBtn").click(function()
    {
        $("#testP").after("<button class='removeButton'><b>x</b></button>");           
    });

    $('.removeButton').live('click', function(){
        alert('test');
    });


    });

</script>
{% endblock %}
{% block content %}
<div class="testArea">
    <div class="inner"></div>
</div>
<p id="testP">test</p>
<form>
<table id="table">
    <tbody>
        <tr>
            <td><button type="button" class="addRowBtn" id="addRowBtn"><b>+</b></button></td>
        </tr>
    </tbody>
</table>
</form>
{% endblock %}

谢谢,

【问题讨论】:

    标签: jquery django django-templates


    【解决方案1】:

    use .on() live 自 1.9 以来已被删除,而您正在使用 1.10

    $("#testP").on('click','.removeButton', function(){
        alert('test');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 2016-08-20
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多