簡體   English   中英

檢查是否已選中所有單選按鈕

[英]Check if all radio buttons are checked

我想檢查一些單選按鈕,以允許用戶點擊一個鏈接(否則它應該對一個allert上訴)。

這是代碼

<html>
<head>
</head>
<body>
<a href="http://www.aerosoft.de" id="a_next">aerosoft.de</a>

            <li class="radiobutton"><span class="name">Struct. Temp. Indic.> 38°C -not exceed 55°C</span>
            <input name="1" type="radio" value="other" /></li>
            <li class="radiobutton"><span class="name">Airplane Documents - check </span>
            <input name="2" type="radio" value="other" /></li>
            <li class="radiobutton"><span class="name">Flight Control Lock - removed</span>
            <input name="3" type="radio" value="other" /></li>
</body>
</html>

用戶必須檢查所有3個Radiobuttons以使鏈接工作,否則,如果他只檢查2個radiobutton,他應該在他點擊鏈接時收到警報。

如果有人可以提供幫助,那會很棒:/

向法比安致敬

處理鏈接上的click事件。 循環通過無線電,如果沒有選中,則顯示警報並返回false以取消點擊的默認操作(即取消導航)。

window.onload = function() {
    document.getElementById("a_next").onclick = function(e) {
        if (!e) e = window.event;
        var els = document.getElementsByTagName("input"),
            i;
        for (i=0; i < els.length; i++) {
            if (!els[i].checked) {
                alert("Your message here.");
                e.returnValue = false;
                return false;
            }
        }
    };
};

演示: http//jsfiddle.net/t9wqc/

如果你可以使用jQuery,它更清潔..

在此處查看完整示例

<a href="#">aerosoft.de</a>


var link = "http://www.aerosoft.de" ;

$('input').change(function(){
   var allChecked = $('input:checked').length == 3;

    if(allChecked ){
      $('#a_next').attr('href',link).removeClass('disabled');
    }
    else{
      $('#a_next').attr('href','#').addClass('disabled');
    }             
});​

如果你可以使用jQuery,

編輯:(評論后;以前的答案有錯誤)

$(document).ready(function() {
    $('#a_next').click(function(e){                 
        $('input[type="radio"]').each(function(){
            if(!$(this).prop('checked')) {
               alert('Check all radios first');
               e.preventDefault();  // to stop anchor jumping to url
               return false;      // to break .each
            }
        }); 
    });
});

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM