簡體   English   中英

如何驗證JavaScript中的多個復選框選擇?

[英]How to validate multiple check box selection in a JavaScript?

如何驗證JavaScript中的多個復選框選擇?

使用此語法無法正確觸發。

(document.getElementById('Other1').checked || document.getElementById('Other2').checked || document.getElementById('Other').checked)

我想,這個問題與變更功能有關。 如何在更改函數中包含OR邏輯運算符。

下面的語法非常適合選擇一個復選框。

$(document).ready(function () 
{
     $('#Other').change(CheckBoxOtherChange);

});


function CheckBoxOtherChange() 
{
    if (document.getElementById('Other').checked) {
        document.getElementById('Notes').disabled = false;
    }
    else {
        document.getElementById('Notes').disabled = true;
        document.getElementById('Notes').value = "";
    }
}

用這個..

$('#someButton').click(function() {
    var values= [];
    $('#MyDiv input:checked').each(function() {
        values.push(this.val);
    });
    // now values contains all of the values of checked checkboxes
    // do something with it
});

檢查這個

 function myfunc() { var check = document.getElementById('Other2').checked || document.getElementById('Other').checked; document.getElementById('Notes').disabled = !check; if (!check) document.getElementById('Notes').value = "" } myfunc(); 
 <input type="checkbox" id="Other2" onchange="myfunc()"/> <input type="checkbox" id="Other" checked onchange="myfunc()"/> <input type="text" id="Notes" value="foo"/> 

您可以使用下面的代碼行為字典提供鍵,將鍵作為id,將值作為'true'或'false'

$('#click').click(function () { //#click refers to id of a button
            var dict = [];
            $('input[type="checkbox"]').each(function () {
                dict.push({
                    key: $(this).attr('id'),
                    value: $(this)[0].checked
                });
            });
        });

    });

暫無
暫無

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

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