簡體   English   中英

使用HTML單選按鈕更改內容

[英]Change content with HTML radio buttons

我的網站上有以下代碼。

<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>

我希望當用戶打開頁面時顯示“含增值稅價格”。 不幸的是,它不適用於僅“檢查”表單。

你們當中有人知道如何解決嗎? :)

在document.ready上,通過JavaScript檢查廣播。

您可以為此使用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();
}

這是一個小提琴

不要忘記將id分別作為vat和novat添加到您的單選按鈕中。

如果您想使用jQuery,則可以使用它的ready()函數,該函數將在頁面加載時執行,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM