简体   繁体   English

用jQuery隐藏动态附加的div /隐藏不起作用

[英]Hide a dynamic appended div with jQuery / hide don't work

I´m trying to hide a appended div with jQuery using hide() but is not working. 我正在尝试使用hide()使用jQuery隐藏附加的div,但无法正常工作。

I'm append the button who trigger the action too. 我添加了触发动作的按钮。

This is the button: 这是按钮:

<button type="button" class="btn btn-danger btn-xs" name="'+response[i][0]+'" id="studentContactDeleteButton" style="width:110px">Eliminar registro</button>

Here is the complete code of the function (including the div and the button): 这是该功能的完整代码(包括div和按钮):

With this I append divs to my page depending of the number of my records. 通过此操作,我可以根据记录数将div附加到页面上。

var formData = {type:"contact",id:id};
$.ajax({
    type: "POST",
    data : formData,
    url: "./content/studentsData.php", 
    dataType: 'json',
    success: function(response){
        for(var i = 0; i < response.length; i++) {
            $("#contactData").append('<div id="studentContactContainer'+response[i][0]+'" style="margin-bottom:-12px;border-top:1px solid #ddd;padding:16px 0px;">');
            $("#contactData").append('<div class="form-group"><label class="col-sm-4 control-label">Descripción:</label><div class="col-sm-7 text-primary" id="div-studentContactDesc'+response[i][0]+'"><input type="text" style="font-size:12px" class="form-control" value="'+decodeHtml(response[i][1])+'" id="studentContactDesc'+response[i][0]+'" placeholder="Descripción"></div><div class="col-sm-1">&nbsp;</div></div>');
            $("#contactData").append('<div class="form-group"><label class="col-sm-4 control-label">Teléfono:</label><div class="col-sm-7 text-primary" id="div-studentContactPhone'+response[i][0]+'"><input type="text" style="font-size:12px" class="form-control" value="'+decodeHtml(response[i][2])+'" id="studentContactPhone'+response[i][0]+'" placeholder="Teléfono"></div><div class="col-sm-1">&nbsp;</div></div>');
            $("#contactData").append('<div class="form-group"><div class="col-sm-1">&nbsp;</div><div class="col-sm-3 text-primary"><button type="button" class="btn btn-success btn-xs" name="'+response[i][0]+'" id="button-test" style="width:110px">Guardar cambios</button></div><div class="col-sm-3 text-primary"><button type="button" class="btn btn-danger btn-xs" name="'+response[i][0]+'" id="studentContactDeleteButton" style="width:110px">Eliminar registro</button></div><div class="col-sm-3 text-primary"><button type="button" class="btn btn-primary btn-xs" id="" style="width:110px">Restablecer</button></div><div class="col-sm-2">&nbsp;</div></div>');
            $("#contactData").append('</div>');
        }
    }
}); 

And with this I´m trying to hide the div but is not working 为此,我试图隐藏div但无法正常工作

$(document).on('click','#studentContactDeleteButton',function(){
  var id = $(this).attr("name");
  $("#studentContactContainer"+id).hide();
});

The click works fine because im getting the value of the id ( response[i][0] have the value of the id) but $("#studentContactContainer"+id).hide(); 该点击效果很好,因为我正在获取ID的值( response[i][0]具有ID的值),但是$("#studentContactContainer"+id).hide(); is not working, only hide the border top of the div but not all. 不起作用,仅隐藏div的边框顶部,而不是全部隐藏。

Hope somebody have an idea. 希望有人有一个主意。

Thanks. 谢谢。

You are using .append function in a wrong way. 您正在错误地使用.append函数。 You have to pass a complete div as parameter and not in different parts. 您必须传递完整的div作为参数,而不是不同的部分。

$("#contactData").append('<div id="studentContactContainer'+response[i][0]+'"></div>');

and after add all groups to your new contactContainer like this: 然后将所有组添加到新的contactContainer中,如下所示:

$("#studentContactContainer"+id).append(<div class="form-group"></div>)
                                .append(<div class="form-group"></div>);
                                .append(<div class="form-group"></div>);

Your first append statement in your loop is appending the container, but the subsequent append statements are not appending the specified divs to the new container. 循环中的第一个append语句将追加容器,但随后的append语句未将指定的div追加到新容器。

I think you want something more like this 我想你想要更多这样的东西

$('<div id="studentContactContainer'+response[i][0]+'"/>')
    .appendTo('#contactData')
    .append('<div>child div 1</div>')
    .append('<div>child div 2</div>');

For better performance, compose the entire html string before appending it, like this 为了获得更好的性能,请在附加整个html字符串之前将其组成,如下所示

var html = '<div id="studentContactContainer'+response[i][0]+'"><div>child 1</div><div>child 2</div></div>';
$('#contactData').append(html);

For easier maintainability, you may want to consider a template engine like Handlebars . 为了更容易维护,您可能需要考虑使用诸如Handlebars之类的模板引擎。

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

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