简体   繁体   中英

Change content with HTML radio buttons

I have the following code on my website.

<input type="radio" name="pricelist" onclick="online();" /> Show price with VAT
<br />
<input type="radio" name="pricelist" onclick="offline();" /> Show price without VAT
<br />
<span id="update"></span>
<script type="text/javascript">
    function online(){
        document.getElementById("update").innerHTML = "100$";
    }
    function offline(){
        document.getElementById("update").innerHTML = "75$";
    }
</script>

I would like the "Show price with VAT" to be there when the user opens the page. It doesn't work with just "checked" for the form, unfortunately.

Do anyone of you have any idea how to solve this? :)

在document.ready上,通过JavaScript检查广播。

You can use onload event for this,

    <body onload="initial()">
       <input id="vat" type="radio" name="pricelist" onclick="online();" /> Show price with VAT
       <br />
       <input id="novat" type="radio" name="pricelist" onclick="offline();" /> Show price without VAT
       <br />
    </body>

function initial(){

   //automatically check the radio button
   document.getElementById("vat").checked = true;

   //call the function you want to call when page loads
   online();
}

Here is a fiddle for it

Don't forget to add an id to your radio buttons as vat and novat respectively.

If you want to use jQuery then you can use it's ready() function which will be executed when your page loads,

$(document).ready(function(){
   initial();
});

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