简体   繁体   English

如果没有选中单选按钮,我该怎么做

[英]how can i do a function when radio button is not checked

i wrote the below code to check if radio button is not checked do another function .but the code does not work here is my snippet : 我写了下面的代码来检查是否没有选中单选按钮做另一个功能。但是代码不起作用是我的摘录:

 $('.multiway').change(function(){ if ($('.multiway').is(':checked')){ $("#cityadd").removeClass("multiflight"); $("#cityadd").addClass("multiflight-checked"); } else{ if ($('.multiway').not(':checked')){ $("#cityadd").removeClass("multiflight-checked"); $("#cityadd").addClass("multiflight"); }} }); 
 <!doctype html> <html> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <style> .multiflight{display:none !important;} .multiflight-checked{display:block !important;} </style> </head> <body> 1 <input type="radio" class="multiway" name="ways"> 2 <input type="radio" name="ways"> <div class="newcity multiflight" id="cityadd">HELLOO</div> </body> </html> 

You need to bind the event to the ways input, instead of to the specific buton: 您需要将事件绑定到ways输入,而不是特定的按钮:

$('input[name="ways"]').change(function(){
   if ($('.multiway').is(':checked')){
       $("#cityadd").removeClass("multiflight");
       $("#cityadd").addClass("multiflight-checked");
   }
   else{
    if ($('.multiway').not(':checked')) {
        $("#cityadd").removeClass("multiflight-checked");
        $("#cityadd").addClass("multiflight");
    }
   }
});

https://jsfiddle.net/6xhcfc56/1/ https://jsfiddle.net/6xhcfc56/1/

But you can make things more short and elegant, by adding a value attribute to the form and question it: 但是,您可以通过向表单添加value属性并对其进行质疑来使事情变得更简洁明了:

HTML: HTML:

<input type="radio"  class="multiway" name="ways" value="0">1
<input type="radio"  name="ways" value="1">2

JS: JS:

$('input[name="ways"]').change(function(){
  var val = $(this).val();
  if(val == 0)
    $('#cityadd').removeClass('multiflight')
  else
    $('#cityadd').addClass('multiflight')
});

https://jsfiddle.net/f15LnedL/2/ https://jsfiddle.net/f15LnedL/2/

 $('.multiway').change(function(){
     if ($(this).prop('checked')){ 
    $("#cityadd").removeClass("multiflight"); 
    $("#cityadd").addClass("multiflight-checked"); } 
else{    
    $("#cityadd").removeClass("multiflight-checked"); 
    $("#cityadd").addClass("multiflight");  
    }
     });

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

相关问题 选中单选按钮时如何获取值? - How do I get values when radio button is checked? 我该怎么做:选中单选按钮时需要更改输入? - How can I do it : When checked radio button change input required? 如果选中单选按钮,如何获取单选按钮的文本? - How can I get the text of radio button if radio button checked? Javascript:当使用同一类检查其他 15 个单选按钮时,如何启用单选按钮? - Javascript: How can I enable a radio button when 15 other radio buttons are checked with the same class? 如何在没有提交按钮的情况下检查单选按钮是否被选中? - How can i check if the radio button is checked or not without a submit button? 选中单选按钮时我无法隐藏/显示文本框 - I Can't Hide/Show Textbox when radio button is checked 如何仅在选中单选按钮时触发 onlick() 函数? - How to fire onlick() function only when a radio button is checked? 单选按钮从选中状态变为未选中状态时该怎么做? - How to do something when radio button is going from checked to unchecked? 如何在页面加载时根据选中的单选按钮禁用文本区域? - How do I disable text areas based on what radio button is checked when the page loads? 选中分配给它的单选按钮时,如何使输入/文本字段为必填字段? - How do I make an input/text field mandatory when the radio button assigned to it is checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM