javascript - jquery div 不响应其他 javascript/jquery 方法

标签 javascript jquery html css

我使用 $(select).append() 方法在 div 中添加了一个 div,但我希望它在单击图像时关闭它,所以我添加了图像和另一个 $(select).button()。点击({ $(select).hide() });但是单击关闭图像时没有任何反应....

$(document).ready(function() {
            $("#subCre").click(function() {
                cust = $("#customer").val();
                address = $('#address').val();
                phone = $('#phone').val();
                amt = $('#initamount').val();
                user = '<%= session.getAttribute("user").toString()%>';
                type = '<%=session.getAttribute("type").toString()%>';
                alert(cust + address + phone + amt + user + type);
                $.post("../processor/proc2.jsp",
                        {
                            customer: cust,
                            address: address,
                            phone: phone,
                            initamount: amt,
                            user: user,
                            type: type
                        },
                function(data, status) {
                    if (status === "success") {
                        if(data.length == 11) {
                            $("#SystemMessages").append('<div class="Msg draggable"><img class="close" src="../mime/close.png">Account Added</div>'); // **Here one div
                        } else {
                            $("#SystemMessages").append('<div class="errorMsg draggable"><img class="close" src="../mime/close.png">'+ data + '</div>'); //** here 2nd div
                      }
                        $("#customer").val("");
                        $('#address').val("");
                        $('#phone').val("");
                        $('#initamount').val("");
                    }
                });

            $(".close").button().click(function() {
                $(".close").parent().css("visibility", "hidden");
            });
       });

最佳答案

你需要对动态追加的元素使用事件委托(delegate)

$('#SystemMessages').on('click','.close',function(){
    $(this).parent().css("visibility", "hidden");
}); 

事件委托(delegate)---> https://learn.jquery.com/events/event-delegation/

关于javascript - jquery div 不响应其他 javascript/jquery 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23059167/

相关文章:

html - 我如何将引导按钮放在另一个相对位置的 div 上?

javascript - ReactJS 怎么了 'import' 和 'export' 可能只出现在顶层?

javascript - Bootstrap 3 Navbar 在 iPhone 上移出屏幕

javascript - 在 JavaScript 中删除小数以获得自动结果

javascript - 用于获取 div id 文本的 Jsoup 选择器

html - 两个带有点概念的类选择器有什么区别?

html - 文档模式下表格边缘的 CSS 圆 Angular 问题 IE7 标准

javascript - Internet Explorer 8 chop iframe 内容

javascript - 无法执行ajax post操作

jQuery <select> 第一个 <option> 未禁用