所以我是JavaScript新手。我正在嘗試創建一個簡單的代碼,允許用戶選擇binary
,hex
或oct
並將十進制數轉換爲所選的單選按鈕。現在我並不擔心轉換,我只是想在代碼提交後運行一個函數。例如,如果選擇單選按鈕二進制,一旦點擊提交按鈕,在頁面上應該說「二進制選擇」。提交的JavaScript單選按鈕
我真的失去了如何讓這個工作,並不知道在哪裏看。我一直在更改我的代碼,當它選擇它生成「對象選定」按鈕時,它就起作用了,但我希望它在按下提交按鈕後才生成。不確定是否必須編寫提交按鈕或表單標籤。我現在,非工作代碼如下:
<!DOCTYPE html>
<html>
\t <head>
\t \t <script>
\t \t function convertit()
\t \t {
\t \t \t
\t \t \t var conversion = document.getElementsByName('convert');
\t \t \t if (conversion[0].checked == true)
\t \t \t {
\t \t \t \t document.getElementById('test').innerHTML = "Binary Checked";
\t \t \t }
\t \t \t else if (conversion[1].checked == true) \t
\t \t \t {
\t \t \t \t document.getElementById('test').innerHTML = " Hex Checked";
\t \t \t }
\t \t \t else if (conversion[2].checked == true) \t
\t \t \t {
\t \t \t \t document.getElementById('test').innerHTML = "Oct Checked";
\t \t \t }
\t \t \t else
\t \t \t {
\t \t \t \t alert("Empty");
\t \t \t }
\t \t \t return true;
\t \t }
\t
\t
\t \t </script>
\t </head>
\t <body>
\t \t <p id="test"> </p>
\t \t <form onsubmit="return convertit();">
\t \t \t <input type="radio" value="binary" name="convert" id="binarybut" >
\t \t \t \t Binary
\t \t \t </input>
\t \t \t <input type="radio" value="binary" name="convert" id="hexbut" >
\t \t \t \t Hex
\t \t \t </input>
\t \t \t <input type="radio" value="hex" name="convert" id="octbut">
\t \t \t \t Oct
\t \t \t </input>
\t \t \t <input type="submit" value="Convert Number" >
\t \t </form>
</body>
</html>
你的代碼是好的,只是從處理程序返回'FALSE'使默認表單提交將被阻止。您的表單正在被提交,導致頁面刷新,因此它將進入默認狀態 –
請參閱https://jsfiddle.net/arunpjohny/tm1aoukx/1/ –
非常感謝!很高興這是一個簡單的解決方法,我一整天都在瘋狂地試圖讓它工作。謝謝!!!有效! – FckItsChey