我建议添加一个自定义属性来跟踪每个无线电的先前状态,如下所示:
$(function(){
$('input[name="rad"]').click(function(){
var $radio = $(this);
// if this was prevIoUsly checked
if ($radio.data('waschecked') == true)
{
$radio.prop('checked',false);
$radio.data('waschecked',false);
}
else
$radio.data('waschecked',true);
// remove was checked from other radios
$radio.siblings('input[name="rad"]').data('waschecked',false);
});
});
您还需要将此属性添加到最初检查的无线电标记中
dio" name="rad" id="Radio0" checked="checked" data-waschecked="true" />
更新:
$(function(){
$('input[name="rad"]').click(function(){
var $radio = $(this);
// if this was prevIoUsly checked
if ($radio.data('waschecked') == true)
{
$radio.prop('checked',false);
$radio.data('waschecked',false);
}
else
$radio.data('waschecked',true);
// remove was checked from other radios
$radio.siblings('input[type="radio"]').data('waschecked',false);
});
});
但请确保您没有其他无线电组可以使用,否则您必须提供一些属性来指定这些按钮,就像我之前关注的名称一样.