jquery - 将复选框(多项选择)转换为单选按钮或单选复选框?

标签 jquery forms

我有以下复选框列表。列表的输出方式允许用户选择多个值。但是,我需要只允许用户从下面的复选框列表中选择一个值。

是否可以通过 jQuery 以某种方式实现这一点?将列表更改为单选按钮、选择列表或其他可能有帮助的内容也可以。

不幸的是,我无法访问生成函数,因此无法更改输出。

提前致谢

<form class="ajax-form" id="user-register" method="post" accept-charset="UTF-8" action="(...)">

(...)

<div id="edit-notifications-custom-custom-berlin-wrapper" class="form-item form-option">
    <label for="edit-notifications-custom-custom-berlin" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-berlin" name="notifications_custom[custom_berlin]"> Berlin</label>
</div>

<div id="edit-notifications-custom-custom-hamburg-wrapper" class="form-item form-option">
    <label for="edit-notifications-custom-custom-hamburg" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-hamburg" name="notifications_custom[custom_hamburg]"> Hamburg</label>
</div>

<div id="edit-notifications-custom-custom-frankfurt-am-main-wrapper" class="form-item form-option">
    <label for="edit-notifications-custom-custom-frankfurt-am-main" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-frankfurt-am-main" name="notifications_custom[custom_frankfurt-am-main]"> Frankfurt am Main</label>
</div>

<div id="edit-notifications-custom-custom-duesseldorf-wrapper" class="form-item form-option">
    <label for="edit-notifications-custom-custom-duesseldorf" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-duesseldorf" name="notifications_custom[custom_duesseldorf]"> Düsseldorf</label>
</div>

<div id="edit-notifications-custom-custom-bielefeld-wrapper" class="form-item form-option">
    <label for="edit-notifications-custom-custom-bielefeld" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-bielefeld" name="notifications_custom[custom_bielefeld]"> Bielefeld</label>
</div>

<div id="edit-notifications-custom-custom-muenchen-wrapper" class="form-item form-option">
    <label for="edit-notifications-custom-custom-muenchen" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-muenchen" name="notifications_custom[custom_muenchen]"> München</label>
</div>

(...)

阿斯达德

最佳答案

$('.form-checkbox').bind('click', function() {
    $('.form-checkbox').not(this).attr('checked', false);
});

已编辑以优化性能。

关于jquery - 将复选框(多项选择)转换为单选按钮或单选复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4081541/

相关文章:

javascript 表单验证未按预期工作

javascript - 解除绑定(bind)实际上不起作用 : is there any other solution?

javascript - 使用 jQuery 显示选择查询结果

javascript - 使用 toUpperCase 方法使 p 元素以大写形式显示

javascript - 在提交 IFRAME 表单之前,将父 url 保存到隐藏字段

javascript - 在不更改 url 页面的情况下发送表单

c# - 单引号 - 我如何输出它并在源代码中看到它而不是'

javascript - (!= 和 == 等) 的 jQuery 解释

javascript - 表单未在新选项卡中打开

c# 将表单添加到项目: Possible to make it internal/private from designer?