2016-10-24 44 views
1
$('.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> 

我想檢查/取消選中此單選按鈕,當我點擊它,並且想要相應地設置條件。如何檢查/取消單個單選按鈕

+1

使用複選框此 – guradio

+0

不,我的設計模式需要單選按鈕。 – neha910

+0

要取消選中'radio',你需要創建另一個收音機,其值爲'no',最好使用checkbox作爲@guradio建議的 – Satpal

回答

1

試試這個:

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(); 
 
    } 
 
});
<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>

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>

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>

+0

更好的解決方案,但我仍然建議使用「複選框」 – Satpal