追加后jquery事件无法正常工作 [英] jquery event not working after append

查看:80
本文介绍了追加后jquery事件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/YsnhT/2/

追加后Jquery事件开启不起作用。单击保存按钮后,我需要textarea的值。

Jquery event on is not working after the append. I need the value of textarea after I click save button.

$('.span8')
    .on('click',
            '.btn',
            function() {
        var input = $("#textarea").val();
        alert(input);
    });

$('body').on('click','#createNote',function() {                $('.span8').empty();
                $('.span8').append('<button type="button" class="btn" style="float: right; margin-right: 8px;background-color:#dddddd;font-family:Roboto Slab;color: black" id="save" data-loading-text="Saving...">Save</button><div class="hero-unit"><input type="text" style="width: 100%" id="title" placeholder="Title"/>'+ 
                '<textarea contenteditable="true" id="textarea" class="textarea" placeholder="Enter text ..."style="width: 100%;">dd</textarea></div>');

            });

HTML:

<div class="span8"></div>


推荐答案

由于 #save 是动态创建的,使用以下处理程序:

Since #save is dynamically created, use the following handler:

$(document).on('click', '#save' ,function() {
    //do stuff
});

更新小提琴:http://jsfiddle.net/tymeJV/YsnhT/3/

您还应该像这样制作点击按钮处理程序,或者否则它不会在新创建的 #click 按钮上触发。

You should also make your "click" button handler like this, or else it will not fire on newly created #click buttons.

这篇关于追加后jquery事件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆