1. 程式人生 > >單選框radio單擊取消選中

單選框radio單擊取消選中

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8" />
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js">
    </script>
    <script type="text/javascript">
        $(function(){
            $('input:radio').click(function(){
                var domName = $(this).attr('name');
                var $radio = $(this);
                if ($radio.data('waschecked') == true){
                    $radio.prop('checked', false);
                    $("input:radio[name='" + domName + "']").data('waschecked',false);
                } else {
                    $radio.prop('checked', true);
                    $("input:radio[name='" + domName + "']").data('waschecked',false);
                    $radio.data('waschecked', true);
                }
                console.log("選中狀態:",$(this).prop("checked"));
                console.log($("input:radio[name='" + domName + "']:checked").val());
            });
        });
    </script>
</head>
<body>
<form>
    <input type="radio" name="browser" value="AAAA">AAAA<br />
     <input type="radio" name="browser" value="BBBBB">BBBBB<br />
    <input type="radio" name="browser" value="CCCCCC">CCCCCC<br />
</form>

</body>
</html>