在html表中循环并检查是否选择了收音机

Loop in html table and check if a radio is selected

本文关键字:是否 选择 收音机 检查 html 循环      更新时间:2023-09-26

我想要一个javascript或jquery函数,它可以在html表中搜索,如果没有选择单选,则通知用户。我只有表id,不知道电台id。

DEMO

在代码中,您可以将table替换为#tableID,以便代码仅为该表运行

$(document).ready(function () {
    if ($('table input[type=radio]:checked').length < 1) {
        alert('No Radio Button Selected');
    }
});

如果您想在table上通知,请单击

 $('table').click(function () {
        if ($('table input[type=radio]:checked').length < 1) {
            alert('No Radio Button Selected');
        }
    });

您可以使用返回布尔值的is()

if (!$('table :radio').is(':checked')) {
        alert('No Radio Button Selected');
    }

如果至少选中一个单选按钮,则不会触发alert()

$('#table input:radio:checked') will give the radios checked inside table with id `#table`