简体   繁体   English

Jquery - 删除单选按钮

[英]Jquery - remove radio button

I have two radio buttons with text label.我有两个带有文本标签的单选按钮。 I try to remove the radio button that does not have value then the radio button was remove successfully, but the problem is its text label, I also want to remove them but i can not.我尝试删除没有值的单选按钮然后成功删除单选按钮,但问题是它的文本标签,我也想删除它们但我不能。 Can anyone show me how to do that?谁能告诉我怎么做? Thank you so much for reading my question!非常感谢您阅读我的问题!

Add a class to the label when you remove the button with the name hidden for example.例如,当您删除名称隐藏的按钮时,向标签添加一个类。 Then in the .css set the .hidden { display: none;}然后在 .css 中设置.hidden { display: none;}

you have two options你有两个选择

  1. you can add same class name for both radio and text field and hide the class eg:您可以为广播和文本字段添加相同的类名并隐藏类,例如:

<input type="radio" class="myclass" > <label class="myclass">abc</label> $(".myclass").hide();

2.also you can add input feild and lable to Div,hide div eg:- 2.也可以将输入字段和标签添加到 Div,隐藏 div 例如:-

<div id="mydiv"> <input type="radio" ><label >abc</label> </div> $("#mydiv").hide();

This will work for you, use element.each to find all the radios without value and remove them ...这对你有用,使用element.each找到所有没有价值的收音机并删除它们......

Here in this code, only first radio will be displayed because it has value and the second one is removed and the label for second is also removed...在这段代码中,只显示第一个收音机,因为它有值,第二个被删除,第二个标签也被删除......

 $(document).ready(function(){ $('input[type="radio"]:not([value])').each(function() { var idVal = $(this).attr("id"); $(this).remove(); $("label[for='"+idVal+"']").remove(); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="submit" id="submit" action="#" method="post"> <label for="one">First Item</label> <input type="radio" id="one" name="first_item" value="1" /> <label for="two">Second Item</label> <input type="radio" id="two" name="first_item" /> </form>

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

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