Search code examples
jqueryradio-buttonclick

jquery click on radio button not working


I have an HTML page exactly as below :

<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<script>
$("input:radio[name=aa]").click(function() {
window.location.href="http://www.google.com";
});

function get_search_results(params) {
    alert('sdsds');
}
</script>
<body>
<input type="radio" name="aa" />AA
<input type="radio" name="aa" />AB
<input type="radio" name="aa" />AC
</body>
</html>

So I was expecting whenever I click on any of the three radio buttons, it should redirect to google. But it does nothing ! Why ?


Solution

  • The script needs to be exected when the document is loaded:

    $(document).ready(function(){
        $("input:radio[name=aa]").click(function() {
            window.location.href="http://www.google.com";
        });
    });