如何允许只有一个单选按钮选择,如果他们有不同的名称


How to allow only 1 radio button selected if they have different names

我怎样才能使脚本不允许用户选择各种单选按钮?这是我的代码:

<div class="controls"   >
    <label class="radio"> <input type="radio" <?php echo ($this->state->get('filter.totalinmuebles') == '1') ? 'checked' : ''; ?> name="totalinmuebles" id="totalinmuebles" value="1"> Volumen de inmuebles en venta</label>
    <label class="radio"> <input type="radio" <?php echo ($this->state->get('filter.totalventas') == '1') ? 'checked' : ''; ?> name="totalventas" id="totalventas" value="1"> Total de ventas</label>
    <label class="radio"> <input type="radio" <?php echo ($this->state->get('filter.totalventascliente') == '1') ? 'checked' : ''; ?> name="totalventascliente" id="totalventascliente" value="1"> Total de ventas por cliente</label>
    <label class="radio"> <input type="radio" <?php echo ($this->state->get('filter.totalcomisionescobrar') == '1') ? 'checked' : ''; ?> name="totalcomisionescobrar" id="totalcomisionescobrar" value="1"> Total comisiones a cobrar</label>
</div>

我已经尝试了我自己的各种脚本,从别人,我在互联网上发现,但它似乎不起作用。无需单击"提交"按钮即可完成验证。

如果不能添加相同的name属性:

$('.controls input').on('change', function() {
   $(this).parent().siblings().find('input').prop('checked', false);
});

您也可以使用closest方法:

$(this).closest('.controls')
       .find('input[type=radio]')
       .not(this)
       .prop('checked', false);

这将为您完成这项工作:

$('#totalinmuebles, #totalventas, #totalventascliente, #totalcomisionescobrar').click(function(){
    var ar=['#totalinmuebles', '#totalventas', '#totalventascliente', '#totalcomisionescobrar'];
    $.each(ar, function( index, value ) {
       $(value).attr('checked',false);
    });
    $(this).attr('checked',true);
});

工作演示:http://jsfiddle.net/kGhKC/

相关文章: