如何选中/取消选中单个单选按钮

时间:2016-10-24 07:04:18

标签: php jquery html

$('.ssn_byphone').click(function(){
    var ssn_role = document.getElementById('ssn_byphone').checked;                     
    if(ssn_role==true)
    {
        $('#ssn-div').hide();
        $('.ssn_byphone').css('display','block');
        $('#ssn').val('');
    }
    else
    {
        $('#slideUp').show();
    }
});

<input id="ssn_byphone" class="ssn_byphone" type="radio" name="pi[ssn_byphone]" value="Yes"><label for="radio1"> I will provide SSN by phone</label>

我想点击它时检查/取消选中此单选按钮,并希望相应地设置条件。

3 个答案:

答案 0 :(得分:2)

试试这个:

&#13;
&#13;
var checked=false;
$('.ssn_byphone').click(function(){
    checked=!checked;
    $(this).prop("checked",checked);
    if($(this).is(':checked'))
    {
        $('#ssn-div').hide();
        $('.ssn_byphone').css('display','block');
        $('#ssn').val('');
    }
    else
    {
        $('#slideUp').show();
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input id="ssn_byphone" class="ssn_byphone" type="radio" name="pi[ssn_byphone]" value="Yes"><label for="radio1"> I will provide SSN by phone</label>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以放置​​一个复选框而不是单选按钮并更改外观,使其看起来像一个单选按钮

<input id="ssn_byphone" class="ssn_byphone" type="checkbox" name="pi[ssn_byphone]" value="Yes" style="-webkit-appearance: radio; -moz-appearance: radio;-ms-appearance: radio;"><label for="radio1"> I will provide SSN by phone</label>

答案 2 :(得分:0)

检查并取消检查单个无线电按钮

  
var myRadio = document.getElementById('ssn_byphone');
var booRadio;

myRadio.onclick = function(){

    if(booRadio == this){
        this.checked = false;
        booRadio = null;
    }else{
        booRadio = this;
    }
};
<input id="ssn_byphone" class="ssn_byphone" type="radio" name="pi[ssn_byphone]" value="Yes"><label for="radio1"> I will provide SSN by phone</label>