javascript – jQuery Make Radio Button无法点击

前端之家收集整理的这篇文章主要介绍了javascript – jQuery Make Radio Button无法点击前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有可能使单选按钮“不可点击”/“不可选”而不“禁用”它?

出于美学原因,我需要知道这是否可能以及如何实现.

目前的代码如下:

<input name='example' value='1' type='radio'/>
<input name='example' value='2' checked='checked' type='radio'/>
<input name='example' value='3' type='radio'/>
<input name='example' value='4' type='radio'/>
<input name='example' value='5' type='radio'/>

解决方法

要在不使用禁用的情况下保留无线电输入的已检查/未检查状态(这会更简单),您可以使用以下命令:

// select the relevant radio input elements:
$('input[name="example"]')
    // bind a change-event handler using 'on()':
    .on('change',function(){
        // re-select the same elements:
        $('input[name="example"]')
            // set the 'checked' property back to the default (on page-load) state:
            .prop('checked',function(){
                return this.defaultChecked;
            });
    });

JS Fiddle demo.

参考文献:

> Attribute-equals ([attribute="value"]) selector.
> on().
> prop().

猜你在找的jQuery相关文章