具有多个无线组的jQuery,只希望1个组具有;是”;将rest change设置为";否";

具有多个无线组的jQuery,只希望1个组具有;是”;将rest change设置为";否";,jquery,radio,Jquery,Radio,我需要这样做,当任何组被设置为“是”时,其他组被设置为“否”,并且有可能在加载时设置它(因此不一定只是一个click()事件) 我已经能够取消单击所有收音机(这会在提交时引发错误),但无法确定如何将其他收音机设置为“0”值 我可以做的事情…更改/添加类,更改ID(虽然没有设置数量的值,但列表中的地址可能在1到10(或更多)之间,由DB动态生成) 这是html,我不能更改“名称”,因为我没有访问表单处理的权限,只有html输出(样式几乎是其他人的代码) 这是使用引导切换按钮进行显示…因此在幕后它也

我需要这样做,当任何组被设置为“是”时,其他组被设置为“否”,并且有可能在加载时设置它(因此不一定只是一个
click()
事件)

我已经能够取消单击所有收音机(这会在提交时引发错误),但无法确定如何将其他收音机设置为“0”值

我可以做的事情…更改/添加类,更改ID(虽然没有设置数量的值,但列表中的地址可能在1到10(或更多)之间,由DB动态生成)

这是html,我不能更改“名称”,因为我没有访问表单处理的权限,只有html输出(样式几乎是其他人的代码)

这是使用引导切换按钮进行显示…因此在幕后它也会对类进行一些操作(如果选择了指向该标签的单选项,则使标签处于活动或不活动状态)


在输入时,必须为每个组选择
1
0
的值,否则它将抛出错误…当它现在工作时,我们可以提交多个
1
值,这不是最佳值

不可能将未检查的无线电值发送到服务器。因此,诀窍是有一个对应于每个ra的隐藏字段dio并将其值发送到服务器,而不是无线电/复选框。请从给定的代码中获得想法,它未经测试。

<div class="btn-group" data-toggle="buttons">
    <input type="hidden" id="primary-hidden-0" value="1" name="address[0][primary]" />
    <label class="btn btn-primary"><input type="radio" id="primary-0-1" value="1" />Yes</label>
    <label class="btn btn-primary active"><input type="radio" id="primary-0-0" value="0" checked="checked" />No</label>
</div>
<div class="btn-group" data-toggle="buttons">
    <input type="hidden" id="primary-hidden-1" value="1" name="address[1][primary]"/>
    <label class="btn btn-primary active">
    <input type="radio" id="primary-1-1" value="1"  checked="checked" />Yes</label>
    <label class="btn btn-primary"><input type="radio" id="primary-1-0" value="0"/>No</label>
</div>
<div class="btn-group" data-toggle="buttons">
    <input type="hidden" id="primary-hidden-2" value="1"  name="address[2][primary]"/>
    <label class="btn btn-primary"><input type="radio" id="primary-2-1" value="1" />Yes</label>
    <label class="btn btn-primary active"><input type="radio" id="primary-2-0" value="0" checked="checked" />No</label>
</div>


$("input[type=radio]:checked").each(function(){
      $(this).prevAll("input[type=hidden]").val($(this).val());
});

$("input[type=radio]").change(function()
{
    $(this).prevAll("input[type=hidden]").val($(this).val());
    $("input[type=radio]:checked").not(this).each(){
       $(this).prop("checked", false);
       $(this).prevAll("input[type=hidden]").val($(this).val());
    });
});

对
不
对
不
对
不
$(“输入[type=radio]:选中”)。每个(函数(){
$(this.prevAll(“input[type=hidden]”).val($(this.val());
});
$(“输入[type=radio]”)。更改(函数()
{
$(this.prevAll(“input[type=hidden]”).val($(this.val());
$(“输入[type=radio]:选中”)。不是(此)。每个(){
$(this).prop(“选中”,false);
$(this.prevAll(“input[type=hidden]”).val($(this.val());
});
});

基于@billyonecan的工作

$('input[type="radio"][value="1"]').on('change', function() {
   var $x = $('input[name="' + this.name + '"]');
   $('input[type="radio"][value="0"]').not( $x ).prop('checked', true).parent().addClass('active');
   $('input[type="radio"][value="1"]').not( $x ).parent().removeClass('active');
}).filter(':checked').change(); 
谢谢你给我指明了正确的方向


因此,您有三个选项可以选择是/否,并且您希望用户能够仅为一个选项选择是……那么为什么不使用组成一个组的三个单选按钮呢,因此,用户可以通过这些选项选择一个选项…?不幸的是,将引导添加到外部资源会破坏它@billyonecan,@CBroe,我无法更改“name”字段,因为我不控制表单的处理,只控制样式
<div class="btn-group" data-toggle="buttons">
    <input type="hidden" id="primary-hidden-0" value="1" name="address[0][primary]" />
    <label class="btn btn-primary"><input type="radio" id="primary-0-1" value="1" />Yes</label>
    <label class="btn btn-primary active"><input type="radio" id="primary-0-0" value="0" checked="checked" />No</label>
</div>
<div class="btn-group" data-toggle="buttons">
    <input type="hidden" id="primary-hidden-1" value="1" name="address[1][primary]"/>
    <label class="btn btn-primary active">
    <input type="radio" id="primary-1-1" value="1"  checked="checked" />Yes</label>
    <label class="btn btn-primary"><input type="radio" id="primary-1-0" value="0"/>No</label>
</div>
<div class="btn-group" data-toggle="buttons">
    <input type="hidden" id="primary-hidden-2" value="1"  name="address[2][primary]"/>
    <label class="btn btn-primary"><input type="radio" id="primary-2-1" value="1" />Yes</label>
    <label class="btn btn-primary active"><input type="radio" id="primary-2-0" value="0" checked="checked" />No</label>
</div>


$("input[type=radio]:checked").each(function(){
      $(this).prevAll("input[type=hidden]").val($(this).val());
});

$("input[type=radio]").change(function()
{
    $(this).prevAll("input[type=hidden]").val($(this).val());
    $("input[type=radio]:checked").not(this).each(){
       $(this).prop("checked", false);
       $(this).prevAll("input[type=hidden]").val($(this).val());
    });
});
$('input[type="radio"][value="1"]').on('change', function() {
   var $x = $('input[name="' + this.name + '"]');
   $('input[type="radio"][value="0"]').not( $x ).prop('checked', true).parent().addClass('active');
   $('input[type="radio"][value="1"]').not( $x ).parent().removeClass('active');
}).filter(':checked').change();