简体   繁体   中英

radio buttons and passing values

 if(document.getElementById('3w').checked) { alert('3w'); }else if(document.getElementById('6w').checked) { alert('6w'); } 
 <input type="radio" name="pnltype" id="3w" value="3w" /> 3W <input type="radio" name="pnltype" id="6w" value="6w" checked /> 6W 

My problem: this will run perfectly in firefox. For example, if I tick 3w and than choose "reload", I get alert '3w' But not in Chrome. In Chrome although I check 3W, and then manually "reload" the page, 'the alert will be '6w'. What is the reason?

In previous versions I used the onClick option in the radio buttons and this function: function radioClick() { window.location.reload(); } And again - in firefox it behave as expected, not in chrome

Thanks for any help.

remove the checked attribute in the second input.

I feel using jquery will be easy for this kind of things. give it a try http://code.jquery.com/jquery-1.11.1.min.js

<input type="radio" class="any-name" name="pnltype" id="3w" value="3w" /> 3W
<input type="radio" class="any-name" name="pnltype" id="6w" value="6w"/>  6W

$('.any-name').on('click', function(){
    alert($(this).val());
});

http://jsfiddle.net/yasithao3/j1ujhetd/1/

try this without reload it should work:

<script>

function check(){
if(document.getElementById('3w').checked) {
      alert('3w');
    }else if(document.getElementById('6w').checked) {
      alert('6w');
    }
}

</script>
<body onload="check()">
    <input type="radio" name="pnltype" id="3w" value="3w" onclick="check()"/> 3W
    <input type="radio" name="pnltype" id="6w" value="6w" checked  onclick="check()"/>  6W
    </body>

Please refer following code

        <title>Test</title>

        <script>
        function ClickEvent()
        {
            if(document.getElementById('3w').checked) {
              alert('3w');
            }
             if(document.getElementById('6w').checked) {
              alert('6w');
            }
        }
        </script>

        <body>
        <input type="radio" name="pnltype" id="3w" value="3w" onclick=" ClickEvent()" /> 3W
        <input type="radio" name="pnltype" id="6w" value="6w" checked onclick="ClickEvent()" />  6W
        </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM