简体   繁体   English

使用HTML单选按钮更改内容

[英]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, 您可以为此使用onload事件,

    <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. 不要忘记将id分别作为vat和novat添加到您的单选按钮中。

If you want to use jQuery then you can use it's ready() function which will be executed when your page loads, 如果您想使用jQuery,则可以使用它的ready()函数,该函数将在页面加载时执行,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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