Activate radio buttons using checkbox conditions

空扰寡人 提交于 2019-12-25 03:00:53

问题


Basically i'd like to activate radio buttons using a checkbox as per the conditions given below-

  1. If i check the checkbox, the first radio would be checked. If i change to another radio button, the checkbox still checked.

  2. If i uncheck the checkbox, I would like to uncheck any radio button. I mean, the radio buttons need to be checked if the checkbox is checked, and if the checkbox is unchecked, the radio buttons need to be unchecked as well.

I've tried a bunch of partial solutions, but none of them work correctly. :(

My HTML:

<input name="featured_ad" value="1" type="checkbox">condition
<br><br>
<input type="radio" name="ad_pack_id" value="497649">value 1<br>
<input type="radio" name="ad_pack_id" value="497648">value 2<br>
<input type="radio" name="ad_pack_id" value="497647">value 3<br>

Thanks a lot!


回答1:


Here is an example using pure javascript.

Fiddle: http://jsfiddle.net/3bugg14y/1/

HTML:

<input name="featured_ad" value="1" type="checkbox" onclick="resetradio(this)">condition
<br><br>
<input type="radio" name="ad_pack_id" value="497649" onclick="setcheckbox()">value 1<br>
<input type="radio" name="ad_pack_id" value="497648" onclick="setcheckbox()">value 2<br>
<input type="radio" name="ad_pack_id" value="497647" onclick="setcheckbox()">value 3<br>

JS:

function resetradio (checkbox) {
    var radios = document.getElementsByName('ad_pack_id');
    var index = 0, length = radios.length;
    if (checkbox.checked == false) {
        for ( ; index < length; index++) {
            radios[index].checked = false;
        }
    }
    else {
        radios[index].checked = true;
    }
}

function setcheckbox () {
    var checkbox = document.getElementsByName('featured_ad')[0];
    if (checkbox.checked == false) {
        checkbox.checked = true;
    }
}



回答2:


You can make use of removeProp() to unselect radio button and prop() to select checkbox.

$(function(){
  $('input[name=featured_ad]').change(function(){
     if(!$(this).is(":checked"))
       $("input[name=ad_pack_id]").removeProp("checked");
     else
        $("input[name=ad_pack_id]:first").prop("checked",true);
  });

  $('input[name=ad_pack_id]').click(function(){
      $('input[name=featured_ad]').prop("checked",true); 
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input name="featured_ad" value="1" type="checkbox">condition
<br><br>
<input type="radio" name="ad_pack_id" value="497649">value 1<br>
<input type="radio" name="ad_pack_id" value="497648">value 2<br>
<input type="radio" name="ad_pack_id" value="497647">value 3<br>



回答3:


If I understood correctly, You can do something like this:

$(':checkbox').on("click", function(event) {
  if (this.checked)
    $(':radio:first').prop('checked', this.checked);
  else
    $(':radio').prop('checked', this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="featured_ad" value="1">condition
<br>
<br>
<input type="radio" name="ad_pack_id" value="497649">value 1
<br>
<input type="radio" name="ad_pack_id" value="497648">value 2
<br>
<input type="radio" name="ad_pack_id" value="497647">value 3
<br>



回答4:


Check below snippet

function set(event){
    if(event.checked=== false && event.type === "checkbox"){
       var ele = document.querySelectorAll("input[type='radio']");
       for(var i = 0; i<ele.length; i++)
       {
          ele[i].checked = false;
       }
     }
    else if(event.checked=== true && event.type === "checkbox"){
        var ele = document.querySelector("input[type='radio']");
        if(ele){
            ele.checked = true;
        }
    }
   else if (event.type !== "checkbox")
   {
     var ele = document.querySelector("input[type='checkbox']");
     if(ele){
        ele.checked = true;
     }
    }
}

changes in html:

<input name="featured_ad" value="1" type="checkbox" onClick="set(this)">condition
<br><br>
<input type="radio" class="a" name="ad_pack_id" value="497649" onClick="set(this)">value 1<br>
<input type="radio" class="a" name="ad_pack_id" value="497648" onClick="set(this)">value 2<br>
<input type="radio" class="a" name="ad_pack_id" value="497647" onClick="set(this)">value 3<br>



回答5:


Added a property to hide/show the radio buttons based on the checkbox condition.

Fiddle: http://jsfiddle.net/3bugg14y/3/

HTML:

<input name="featured_ad" value="1" type="checkbox" onclick="resetradio(this)">condition
<br><br>
<div id="buttons" style="display:none">
<input type="radio" name="ad_pack_id" value="497649">value 1<br>
<input type="radio" name="ad_pack_id" value="497648">value 2<br>
<input type="radio" name="ad_pack_id" value="497647">value 3<br>
</div>

JS:

function resetradio (checkbox)
{
    var radios = document.getElementsByName('ad_pack_id');
    var index = 0, length = radios.length;
    if (checkbox.checked == false)
    {
        document.getElementById('buttons').style.display='none';
        for ( ; index < length; index++)
        {
            radios[index].checked = false;
        }
    }
    else
    {
        document.getElementById('buttons').style.display='block';
        radios[index].checked = true;
    }
}



回答6:


This should work. Also keeping track of the last selected radio button, after unchecking the checkbox.

var check = document.querySelector('input[type="checkbox"]'),
    radioBtns = Array.prototype.slice.call(document.querySelectorAll('input[type="radio"]')),
    isRadioChecked = false,
    lastCheckedRadio = 0;


check.addEventListener('change', function(e) {
  if (!e.target.checked) {
    radioBtns.forEach(function(elem) {
      elem.checked = false;
    });
    isRadioChecked = false;
  } else {
    if (!isRadioChecked) {
      radioBtns[lastCheckedRadio].checked = true;
    }
  }
}, false);

radioBtns.forEach(function(elem, index) {
  elem.addEventListener('change', function(e) {
    if (e.target.checked) {
      isRadioChecked = true;
      lastCheckedRadio = index;
    } else {
      isRadioChecked = false;
    }
    if (e.target.checked && !check.checked) {
      check.checked = true;
    }
  }, false);
});
<input name="featured_ad" value="1" type="checkbox">condition
<br><br>
<input type="radio" name="ad_pack_id" value="497649">value 1<br>
<input type="radio" name="ad_pack_id" value="497648">value 2<br>
<input type="radio" name="ad_pack_id" value="497647">value 3<br>


来源:https://stackoverflow.com/questions/33215234/activate-radio-buttons-using-checkbox-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!