简体   繁体   English

复选框已选中以删除子div

[英]Checkbox Checked to remove a child div

I'm using jQuery and removing a child div and it's controls from it's parent. 我正在使用jQuery并删除了一个子div,它是来自其父项的控件。 I first attempted to use this method: 我首先尝试使用此方法:

  $("[id^=SelectedUser]:checked").each(function () { //.val() contains numberical index of div to remove. $('#SUser' + $(this).val()).remove(); }); 

But that does not remove the element. 但这不会删除该元素。

I found this works: 我发现这有效:

  $("[id^=SelectedUser]:checked").each(function () { $(this).parent().parent().remove(); }); 

Can anyone explain to me why my original concept does not work? 谁能向我解释为什么我的原始概念不起作用?

$this.val() is returning the value of the check box. $this.val()返回复选框的值。 Are you sure that those numbers are coinciding with your div's ID? 您确定这些数字与div的ID一致吗? Seems to be the only thing I can find "wrong" with your code. 似乎是唯一可以找到您的代码“错误”的东西。 Also your numerical value is in a string format correct not an integer Correct? 另外,您的数值是字符串格式,正确的不是整数,正确吗?

Here is the html. 这是HTML。

This generates the child div tags which contain user information. 这将生成包含用户信息的子div标签。 These are loaded into a static div with an ID of "SelectedSelections." 这些被加载到ID为“ SelectedSelections”的静态div中。

  $("#SelectedSelections").append("<div id='SUser" + counter + "' class='SelectedUsersRow'><div class='RowControl'><input type='checkbox' id='SelectedUser" + counter + "' value='" + index + "' onclick='enableDisableButtons(\\"REMOVESELECTIONS\\");'/></div><div class='RowContent'>" + $("#displayString" + index).html() + "</div>" + "<input type='Hidden' id='slastName" + counter + "' value='" + $("#lastName" + index).val() + "' />" + "<input type='Hidden' id='sCalledName" + counter + "' value='" + $("#CalledName" + index).val() + "' />" + "<input type='Hidden' id='sEmail" + counter + "' value='" + $("#Email" + index).val() + "' />" + "<input type='Hidden' id='sDom" + counter + "' value='" + $("#Dom" + index).val() + "' />" + "<input type='Hidden' id='sID" + counter + "' value='" + $("#ID" + index).val() + "' />" + "<input type='Hidden' id='sPhone" + counter + "' value='" + $("#Phone" + index).val() + "' /></div>"); 

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

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