简体   繁体   English

如何从jQuery中的父项中删除一个HTML标记

[英]how to remove a html tag from the parent in jquery

<td>
    <input type='checkbox' value='-1' name='xxx' checked>XXX
    <input type='hidden' value='-1' name='xxx'>
</td>
<td>
    <input type='checkbox' value='-1' name='xyy' checked>Xyy
    <input type='hidden' value='-1' name='xyy'>
</td>
<td>
    <input type='checkbox' value='-1' name='yyy' checked>yyy
    <input type='hidden' value='-1' name='yyy'>
</td>

In the above code I set a hidden value with same name as on submition of form neither 0 nor 1 is being submitted. 在上面的代码中,我设置了一个隐藏值,其名称与提交0或1时一样。 Now I got a problem that when I click the checkbox I want to remove the input:hidden node and remain will same. 现在我遇到一个问题,当我单击复选框时,我想删除input:hidden节点并保持不变。 But using $("input:hidden").remove() its removing the total hidden values present in the document. 但是使用$("input:hidden").remove()删除文档中存在的总隐藏值。

$("input[name=xxx]:hidden").remove();

You can try this if you have that html structure. 如果您具有html结构,则可以尝试此操作。 It simply removes next hidden input of checked checkbox. 它只是删除选中复选框的下一个隐藏输入。

$("input:checkbox").click(function() {
  $(this).next("input:hidden").remove();
});

If you want to delete them only when checkbox is checked you can add this condition 如果仅在选中复选框时要删除它们,则可以添加此条件

if ($(this).is (':checked'))

try this: 尝试这个:

$("input:checkbox").click(function() { 
$(this).next("input:hidden").remove(); 
}); 

Your solution is needlessly complicated if you ask me. 如果您问我,您的解决方案不必要地复杂。 An <input type='checkbox' value='-1' name='xxx' checked/> returns the value to the backend if it is checked else it returns nothing. <input type='checkbox' value='-1' name='xxx' checked/>如果已选中,则将value返回到后端,否则不返回任何值。 So essentially it does the same thing you are trying to accomplish by putting those extra hidden fields and removing them - a lot more efficiently, I might add. 因此,从本质上讲,通过放置那些额外的隐藏字段并删除它们,可以完成您要完成的同一件事-我可能会更有效地添加它们。

Please do review the code you have written. 请检查您编写的代码。 You may refer here for knowing how checkboxes are handled. 您可以参考此处以了解如何处理复选框。

<td>
    <input type='checkbox' value='-1' name='xyy' class='xyz' checked>Xyy
    <input type='hidden' value='-1' name='xyy'  class='xyz'>
</td>.

$("input.xyy:hidden").remove();

Added a class and managed the requirenment. 添加了一个类并管理了需求。

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

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