简体   繁体   English

输入类型单选按钮检查else语句不在jquery中工作

[英]input type radio button checked else statement not working in jquery

I want to add a class in the parent wrapper of radio button, I did so , it is working but else statement is not workin 我想在单选按钮的父包装中添加一个类,我这样做了,它正在工作但是否则语句不起作用

Here is my code 这是我的代码

<div class="radio">
    <input type="radio" name="name[]" checked>
</div>

<div class="radio">
    <input type="radio" name="name[]">
</div>

This is my JS 这是我的JS

$(document).ready(function(){
  $('input[type="radio"]').on('click',function(){
    if($(this).prop('checked')){
      $(this).parent().addClass('new')
    }
    else {
      $(this).parent().removeClass('new')
    }
  });
})

I want to remove the class new when the radio button is not checked, but it is not working. 我想在未选中单选按钮时删除新类,但它不起作用。 eg http://codepen.io/amitabha197/pen/KzqvWv 例如http://codepen.io/amitabha197/pen/KzqvWv

After webeno and mhodges pointed me to the right direction here is my solution. webenomhodges向我指出正确的方向后,这是我的解决方案。

  $('input[type="radio"]').on('change',function(){
    $(this).parent().addClass('new').siblings().removeClass('new');        
  });

Also point to remember, If at all you had been trying to uncheck a single radio button then know this. 还要注意记住,如果你一直试图取消选中一个单选按钮,那就知道了。

You cannot uncheck a radio button . 您无法取消选中单选按钮 All the clicks you do on it will keep it checked, Use checkbox instead. 您对其执行的所有点击都会对其进行检查, 而是使用复选框。

Why is it impossible to deselect HTML “radio” inputs? 为什么不能取消选择HTML“无线电”输入?

Unable to uncheck radio button 无法取消选中单选按钮

Edited 编辑

For code optimization & modularity. 用于代码优化和模块化。

$(document).ready(function(){
  $('input[type="radio"]').on('click',function(){
    var name = $(this).attr("name");
    $('input[type="radio"][name="'+name+'"]').closest(".radio").removeClass("new");
    $(this).closest(".radio").addClass("new");
  });
});

use this function will solve you problem 使用此功能将解决您的问题

$(document).ready(function(){
  $('input[type="radio"]').on('change',function(){
    $(this).parent().addClass('new');
    $('input[type="radio"]').not(this).parent().removeClass('new');
  });
});

You can include <label> element as parent of <input type="radio"> elements at html . 您可以在html包含<label>元素作为<input type="radio">元素的父元素。

The HTML Label Element ( <label> ) represents a caption for an item in a user interface. HTML标签元素<label> )表示用户界面中项目的标题。 It can be associated with a control either by placing the control element inside the <label> element, or by using the for attribute. 它可以通过将控件元素放在<label>元素内,或者使用for属性与控件关联。 Such a control is called the labeled control of the label element. 这种控制称为标签元素的标记控制 One input can be associated with multiple labels. 一个输入可以与多个标签相关联。

The parent label element will be associated with :checked first child descendant input . label元素将与:checked第一个子后代input相关联。

You can style css :after pseudo element compounded to :checked selector to display blue border when input element is :checked . 您可以设置css样式:after伪元素复合到:checked选择器以在input元素为:checked时显示蓝色border

 div.radio { display: inline-block; } label.radio { display: inline-block; width: 100px; border: 1px solid #000; } label.radio > :checked:after { content: ""; position: relative; display: inline-block; width: 100px; height: 20px; left: -6px; top: -4px; border: 1px solid blue; } 
 <div class="radio"> <label class="radio"> <input type="radio" id="radio1" name="name[]" checked> </label> </div> <div class="radio"> <label class="radio"> <input type="radio" for="radio2" name="name[]"> </label> </div> 


If requirement is to use existing html , jQuery , you can utilize .click() , .toggleCLass() to add, remove "new" className if descendant input element checked property is false at click event, set input checked property to true 如果要求是利用现有htmljQuery ,你可以利用.click() .toggleCLass()来添加,删除"new" className如果后代input元素checked属性是falseclick事件,设定input checked属性为true

 $(document).ready(function() { var radio = $(".radio"); radio.click(function(e) { var input = e.target.querySelector("input[type=radio]"); if (!input.checked) { radio.toggleClass("new"); input.checked = true; } }) }) 
 .radio { display: inline-block; width: 100px; border: 1px solid #000; } .new { display: inline-block; width: 100px; border: 1px solid blue !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div class="radio new"> <input type="radio" name="name[]" checked> </div> <div class="radio"> <input type="radio" name="name[]"> </div> 

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

相关问题 jQuery单选按钮已选中,否则语句不起作用 - jQuery radio button checked, else statement not working jQuery:如果语句帮助,如果(选中了单选按钮)则为Jquery,否则为 - Jquery: If statement help, if (radio button is checked) then Jquery, else jQuery如果可点击标签之前的input [type =“ radio”]没有属性“ checked”,则不添加它,否则不做任何事情 - jQuery if input[type=“radio”] before clickable label hasn't attribute “checked” then do NOT add it, else do nothing 如何知道是否在if else语句中选中了具有特定ID的单选按钮 - How to know if radio button with specific id is checked to use in if else statement 默认情况下选中单选按钮-JQuery事件不起作用 - Radio button checked by default - JQuery event not working 使用JQuery在单选按钮中设置“已检查”无效 - Setting “Checked” in Radio Button using JQuery is not working 如果选中单选按钮或不是JQuery - If radio button is checked or not JQuery 单选按钮被选中的jQuery - Radio button is checked jquery jQuery单选按钮检查添加类,否则如果未选中单选按钮则删除类? - jQuery radio button check add class, else remove class if radio button not checked? 如何在文本框中显示单选按钮输入以及if else语句作为输出 - how to display radio button input with if else statement as output in a text box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM