简体   繁体   English

如何在div标签中添加一些额外的文本

[英]How to append some extra text in the div tag

I am trying to take the label text of the checkbox which are checked and I trying to append in the span tag, 我试图将复选框的标签文本选中,然后尝试在span标签中附加,
When user click the the ok button in the popUp 当用户单击弹出窗口中的“确定”按钮时
Here it is not working. 在这里不起作用。 Here is my jsfiddle 这是我的jsfiddle

My script is: 我的脚本是:

$('.modal-footer .preSuccess').click(function(){            
            var parentElement=$(this).parent().parent().attr('id');
            $('#'+parentElement+ '.modal-body  input[type="checkbox"]').each(function(){
                var pre =$(this).next('label').text();
                if($(this).is(':checked')){
                    $('.'+parentElement+'Pre').append('<span>'+pre+'</span>'); 
                }   
            });

        });

Try 尝试

$('.modal-footer .preSuccess').click(function () {
    var modal = $(this).closest('.modal');
    var parentElement = modal.attr('id'); // issue here, use closest to get a parent matching the selector
    //again issue with the selector
    modal.find('.modal-body  input[type="checkbox"]').each(function () {
        var pre = $(this).next('label').text();
        if ($(this).is(':checked')) {
            $('.' + parentElement + 'Pre').append('<span>' + pre + '</span>');
        }
    });

});

Demo: Fiddle 演示: 小提琴

I took a look at your code and you're missing a space when querying the checkbox: 我查看了您的代码,查询复选框时您缺少空格:

 $('#'+parentElement+ ' .modal-body  input[type="checkbox"]')

It wasn't able to find your checkbox and couldn't find the texts. 它找不到您的复选框,也找不到文本。

Other than that I would advice against the .parent().parent() structure, cause it's very prone to future bugs. 除此之外,我建议不要使用.parent()。parent()结构,因为它很容易出现未来的错误。 Maybe just call the id directly, but I'm not sure how you're using this code. 也许直接调用ID,但是我不确定您如何使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM