请高手帮忙写下面这个功能的JS代码:
要求:
<button type="button" class="active">点击我</button>
<input type="radio" style="display: none">
点点击button按钮的class值为active的时候,input自动赋予check属性,即
<input type="radio" style="display: none" check>谢谢~~~
html

解决方案 »

  1.   


    <button type="button" class="active" onclick='checkedRadio(this)'>点击我</button>
    <input type="radio" id="_radio" >
    <script type="text/javascript">
    function checkedRadio(obj){
    document.getElementById('_radio').checked = obj.className=='active';
    }
    </script>
      

  2.   

    如果是2个按钮如何实现呢?如下所示:
    <button type="button" class="active" onclick='checkedRadio(this)'>点击我A</button>
    <input type="radio" >A
    <button type="button" class="active" onclick='checkedRadio(this)'>点击我B</button>
    <input type="radio" >B
      

  3.   

    <button type="button" class="active" onclick='checkedRadio(this,"radio_a")'>点击我A</button>
    <input id="radio_a" type="radio" >A
    <button type="button" class="active" onclick='checkedRadio(this,"radio_b")'>点击我B</button>
    <input id="radio_b" type="radio" >B
    <script type="text/javascript">
    function checkedRadio(obj,_id){
    document.getElementById(_id).checked = obj.className=='active';
    }
    </script>
      

  4.   

    首先谢谢您。
    还有一个小问题:
    <button class="active">里面的active是通过其他JS控制,默认第一个有active值,当点击的时候,被激活的button才有active。所以,这里也要求<input type="radio">也是默认第一个有checked属性。谢谢你再帮忙
      

  5.   

    <input id="radio_a" type="radio" value="A">A 试一下吧
      

  6.   

    <button type="button" class="active" onclick='checkedRadio(this,"radio_a")'>点击我A</button>
    <input id="radio_a" type="radio" checked="true" >A
    <button type="button"  onclick='checkedRadio(this,"radio_b")'>点击我B</button>
    <input id="radio_b" type="radio" >B
    <script type="text/javascript">
    function checkedRadio(obj,_id){
        document.getElementById(_id).checked = obj.className=='active';
        if(obj.className!='active'){
         alert('激活了');
         obj.className='active';
        }
        
    }
    </script>
      

  7.   

    我的是这样的代码,现在还是不好用:<button type="button" class="btn btn-warning btn-primary memtype active" onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
    <input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
    <button type="button" name="post[regid]" value="5" id="g_5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
    <input type="radio" name="post[regid]" value="5" id="g_5">function checkedRadio(obj,_id){
                document.getElementById(_id).checked = obj.className=='active';
            }
      

  8.   

    我的是这样的代码,现在还是不好用:
    <button type="button" class="btn btn-warning btn-primary memtype active" onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
    <input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
    <button type="button" name="post[regid]" value="5" id="g_5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
    <input type="radio" name="post[regid]" value="5" id="g_5">
    function checkedRadio(obj,_id){
        document.getElementById(_id).checked = obj.className=='active';
    }
      

  9.   

    注意,ID不要有相同的。
    <button type="button" class="btn btn-warning btn-primary memtype active" onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
    <input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
    <button type="button" name="post[regid]" value="5" id="g_5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
    <input type="radio" name="post[regid]" value="5" id="g_5">因为你的class比较多,className获取的是所有的class,所以一开始那个方法不行,加了个hasClass方法来判断。代码如下。<button type="button" class="btn btn-warning btn-primary memtype active" 
    onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
    <input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
    <button type="button" name="post[regid]" value="5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
    <input type="radio" name="post[regid]" value="5" id="g_5">
    <script type="text/javascript">
    function checkedRadio(obj,_id){
    var _cheched = hasClass(obj,'active');
        document.getElementById(_id).checked = _cheched;
        if(!_cheched){
         alert('激活');
         obj.className+=' active';
        }
    }
    function hasClass(obj , _class){
    var _classes = obj.className.split(' ');
    for(var i in _classes){
    if(_classes[i]==_class){
    return true;
    }
    }
    return false;
    }
    </script>
      

  10.   


    这个可以。
    最后一个小请求:
    可不可以省去弹出“激活”的过程,也就是点击button的时候,直接radio被checked?
    谢谢!!!!
      

  11.   


    可不可以省去弹出“激活”的过程
    这个是你的问题,如果你不想要激活,直接把if判断那里的删除了,给button的class添加active就行了。
      

  12.   


    我要点击button的时候,radio自动激活。是这样写不?哪里有问题<script type="text/javascript">
    function checkedRadio(obj,_id){    
        var _cheched = hasClass(obj,'active');
        document.getElementById(_id).checked = _cheched;
        obj.className+=' active';
    }
    function hasClass(obj , _class){
        var _classes = obj.className.split(' ');
        for(var i in _classes){
            if(_classes[i]==_class){
                return true;
            }
        }
        return false;
    }
    </script>
      

  13.   


    <button type="button" class="btn btn-warning btn-primary memtype active" 
    onclick='checkedRadio(this,"g_6");' data-toggle="button">企业</button>
    <input type="radio" name="post[regid]" value="6" id="g_6" checked="true">
    <button type="button" name="post[regid]" value="5" onclick='checkedRadio(this,"g_5");' class="btn btn-warning btn-primary memtype" data-toggle="button">个人</button>
    <input type="radio" name="post[regid]" value="5" id="g_5">
    <script type="text/javascript">
    function checkedRadio(obj,_id){    
        var _cheched = hasClass(obj,'active');    
        if(!_cheched){        
            obj.className+=' active';
        }
        document.getElementById(_id).checked = true;
    }
    function hasClass(obj , _class){
        var _classes = obj.className.split(' ');
        for(var i in _classes){
            if(_classes[i]==_class){
                return true;
            }
        }
        return false;
    }
    </script>
      

  14.   


    <a href="#"><button type="button" id="but1" class="active" onclick='checkedRadio(this)'>点击我A</button></a>
    <label for="btn1"><input type="radio" >A</label>
    <a href="#"><button type="button" id="but1" class="active" onclick='checkedRadio(this)'>点击我B</button></a>
    <label for="btn1"><input type="radio" >B</label>