Javascript 使用复选框条件激活单选按钮

Javascript 使用复选框条件激活单选按钮,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,基本上,我想根据下面给出的条件,使用复选框激活单选按钮- 如果我选中该复选框,则会选中第一个收音机。如果我更改为另一个单选按钮,复选框仍处于选中状态 如果我取消选中复选框,我想取消选中任何单选按钮。我的意思是,如果复选框被选中,单选按钮需要被选中,如果复选框被取消选中,单选按钮也需要被取消选中 我尝试了一系列局部解决方案,但没有一个是正确的( 我的HTML: <input name="featured_ad" value="1" type="checkbox">condition &

基本上,我想根据下面给出的条件,使用复选框激活单选按钮-

  • 如果我选中该复选框,则会选中第一个收音机。如果我更改为另一个单选按钮,复选框仍处于选中状态

  • 如果我取消选中复选框,我想取消选中任何单选按钮。我的意思是,如果复选框被选中,单选按钮需要被选中,如果复选框被取消选中,单选按钮也需要被取消选中

  • 我尝试了一系列局部解决方案,但没有一个是正确的(

    我的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>
    
    条件
    

    值1
    值2
    值3

    非常感谢!

    您可以使用
    removeProp()
    取消选中单选按钮,并使用
    prop()
    选中复选框

    $(函数(){
    $('input[name=featured_ad]')。更改(函数(){
    如果(!$(this).is(“:checked”))
    $(“输入[name=ad_pack_id]”)。移除属性(“选中”);
    其他的
    $(“输入[name=ad\u pack\u id]:first”).prop(“选中”,true);
    });
    $('input[name=ad_pack_id]')。单击(function(){
    $('input[name=featured_ad]')。prop(“选中”,true);
    });
    });
    
    条件
    

    值1
    值2

    值3
    您可以使用
    removeProp()
    取消选中单选按钮,并使用
    prop()
    选中复选框

    $(函数(){
    $('input[name=featured_ad]')。更改(函数(){
    如果(!$(this).is(“:checked”))
    $(“输入[name=ad_pack_id]”)。移除属性(“选中”);
    其他的
    $(“输入[name=ad\u pack\u id]:first”).prop(“选中”,true);
    });
    $('input[name=ad_pack_id]')。单击(function(){
    $('input[name=featured_ad]')。prop(“选中”,true);
    });
    });
    
    条件
    

    值1
    值2

    值3
    您可以使用
    removeProp()
    取消选中单选按钮,并使用
    prop()
    选中复选框

    $(函数(){
    $('input[name=featured_ad]')。更改(函数(){
    如果(!$(this).is(“:checked”))
    $(“输入[name=ad_pack_id]”)。移除属性(“选中”);
    其他的
    $(“输入[name=ad\u pack\u id]:first”).prop(“选中”,true);
    });
    $('input[name=ad_pack_id]')。单击(function(){
    $('input[name=featured_ad]')。prop(“选中”,true);
    });
    });
    
    条件
    

    值1
    值2

    值3
    您可以使用
    removeProp()
    取消选中单选按钮,并使用
    prop()
    选中复选框

    $(函数(){
    $('input[name=featured_ad]')。更改(函数(){
    如果(!$(this).is(“:checked”))
    $(“输入[name=ad_pack_id]”)。移除属性(“选中”);
    其他的
    $(“输入[name=ad\u pack\u id]:first”).prop(“选中”,true);
    });
    $('input[name=ad_pack_id]')。单击(function(){
    $('input[name=featured_ad]')。prop(“选中”,true);
    });
    });
    
    条件
    

    值1
    值2

    值3
    这里是一个使用纯javascript的示例

    小提琴:

    HTML:

    条件
    

    值1
    值2
    值3
    JS:

    功能重置收音机(复选框){
    var radios=document.getElementsByName('ad_pack_id');
    var指数=0,长度=radios.length;
    如果(checkbox.checked==false){
    对于(;索引<长度;索引++){
    收音机[索引]。选中=假;
    }
    }
    否则{
    收音机[索引]。选中=真;
    }
    }
    函数setcheckbox(){
    var checkbox=document.getElementsByName('featured_ad')[0];
    如果(checkbox.checked==false){
    checkbox.checked=true;
    }
    }
    
    下面是一个使用纯javascript的示例

    小提琴:

    HTML:

    条件
    

    值1
    值2
    值3
    JS:

    功能重置收音机(复选框){
    var radios=document.getElementsByName('ad_pack_id');
    var指数=0,长度=radios.length;
    如果(checkbox.checked==false){
    对于(;索引<长度;索引++){
    收音机[索引]。选中=假;
    }
    }
    否则{
    收音机[索引]。选中=真;
    }
    }
    函数setcheckbox(){
    var checkbox=document.getElementsByName('featured_ad')[0];
    如果(checkbox.checked==false){
    checkbox.checked=true;
    }
    }
    
    下面是一个使用纯javascript的示例

    小提琴:

    HTML:

    条件
    

    值1
    值2
    值3
    JS:

    功能重置收音机(复选框){
    var radios=document.getElementsByName('ad_pack_id');
    var指数=0,长度=radios.length;
    如果(checkbox.checked==false){
    对于(;索引<长度;索引++){
    收音机[索引]。选中=假;
    }
    }
    否则{
    收音机[索引]。选中=真;
    }
    }
    函数setcheckbox(){
    var checkbox=document.getElementsByName('featured_ad')[0];
    如果(checkbox.checked==false){
    checkbox.checked=true;
    }
    }
    
    下面是一个使用纯javascript的示例

    小提琴:

    HTML:

    条件
    

    值1
    值2
    值3
    JS:

    功能重置收音机(复选框){
    var radios=document.getElementsByName('ad_pack_id');
    var指数=0,长度=radios.length;
    如果(checkbox.checked==false){
    对于(;索引<长度;索引++){
    收音机[索引]。选中=假;
    }
    }
    否则{
    收音机[索引]。选中=真;
    }
    }
    函数setcheckbox(){
    var checkbox=document.getElementsByName('featured_ad')[0];
    如果(checkbox.checked==false){
    checkbox.checked=true;
    }
    }
    
    如果我理解正确,您可以这样做:

    $(':checkbox')。在(“单击”)上,函数(事件){
    如果(选中此项)
    $(':radio:first').prop('checked',this.checked);
    其他的
    $(':radio').prop('checked',this.checked);
    });
    
    条件
    

    值1
    价值2
    价值3

    如果我理解正确,Y
    <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>
    
    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;
        }
    }
    
    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;
         }
        }
    }
    
    <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>
    
    <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>
    
    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;
        }
    }