只选择一个复选框

Ren*_*ene 3 html javascript checkbox

我想构建一个javascript,以便用户只能选择下面提到的一个选项.你可以给我一些指示如何做到这一点,因为我是一个javascript noob.

谢谢!

这是菜单部分的图片 在此输入图像描述

<td><label for="dock_books_search_visible_online"> Visible online?</label></td>
                        <td><input type="checkbox" name="option" value="checkedVisibleOk" id="dock_books_visible_ok"  /> </td>
                        <td><label for="dock_books_search_visible_online_yes"> Yes</label></td>
                        <td><input type="checkbox" name="option" value="checkedVisibleNok" id="dock_books_visible_nok" /> </td>
                        <td><label for="dock_books_search_visible_online_no"> No</label></td>
Run Code Online (Sandbox Code Playgroud)

Alp*_*ale 9

对于从多个选项中进行单选,我们Radio Buttons不使用CheckBoxes.

你应该使用这样的东西.

<input type="radio" name="option" value="Yes" id="yes" /> 
<input type="radio" name="option" value="No" id="no" /> 
Run Code Online (Sandbox Code Playgroud)

但是如果你想反过来,只需在你的head标签中添加以下脚本:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(':checkbox').bind('change', function() {
        var thisClass = $(this).attr('class');
        if ($(this).attr('checked')) {
            $(':checkbox.' + thisClass + ":not(#" + this.id + ")").removeAttr('checked');
        }
        else {
            $(this).attr('checked', 'checked');
        }
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

这是上面的小提琴.

希望这可以帮助.