[英]Makes checkbox like radiobutton WITH uncheck available
我需要有2個復選框作為單選按鈕,但是我不能使用單選按鈕,因為我想取消選中
<script type="text/javascript">
$(document).ready(function() {
$(".gps-garmin").click(function() {
selectedBox = this.id;
$(".gps-garmin").each(function() {
if ( this.id == selectedBox )
{
this.checked = true;
}
else
{
this.checked = false;
};
});
});
});
<td><input type="checkbox" id="garmin" value="garmin" class="gps-garmin"></td>
<td><input type="checkbox" id="gps" value="gps" class="gps-garmin"></td>
實際上,我的復選框的工作方式與單選按鈕完全一樣,我在這里找到了此代碼,但是我需要取消選中
嘗試這個:
$(".gps-garmin").click(function() {
var check = $(this).prop('checked');
$(".gps-garmin").prop('checked',false);
$(this).prop('checked', (check) ? true : false);
});
如How-to-uncheck-a-radiobutton中所述 ,您應該能夠通過使用純js或jQuery清除單選按鈕
例如,在使用普通js的情況下(從鏈接的答案中復制):
this.checked = false;
這是您要嘗試的嗎:$(“。gps-garmin”)。click(function(){
if($(this).prop(“ checked”)== true){
$(“。gps-garmin”)。prop('checked',false);
$(this).prop('checked',true);
} else if($(this).prop(“ checked”)== false){
$(".gps-garmin").prop('checked',false);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.