我遇到了問題,而且 javascript 不是我的強項。我正在制作一個表格,用于在游戲中保留玩家統計資料,并試圖自動化一些數學運算,這些數學運算將在每次玩家輪到玩家時通過點擊完成。
我設法撰寫了第一個代碼以在文本框中使用 on-blur 來增加或減少最大生命值,因為衣物被使用和脫掉,這段代碼作業得很好,所以我嘗試根據玩家輪到的力量來改變它點擊一個按鈕。它基本上是為以前作業過的不同欄位設定的相同代碼,但不是對文本框進行模糊處理,我只是設定一個按鈕來在單擊時運行代碼。
<button onclick="pta()">Click on your turn</button>
在所有文本框重置為空或默認之前,我看到它作業了一瞬間。我使用的 javascript 是
<script>
function pta()
{
var pass1 = parseInt(document.getElementById("pass1").value);
var pass2 = parseInt(document.getElementById("pass2").value);
var pass3 = parseInt(document.getElementById("pass3").value);
var pass4 = parseInt(document.getElementById("pass4").value);
var car1 = parseInt(document.getElementById("car1").value);
var car2 = parseInt(document.getElementById("car2").value);
var car3 = parseInt(document.getElementById("car3").value);
var car4 = parseInt(document.getElementById("car4").value);
document.getElementById("aura").value = car1 car2 car3 car4 - pass1 pass2 - pass3 - pass4;
}
</script>
通過使正確的數字出現在光環文本框中,我看到代碼在很短的時間內作業,至少對于加法,還沒有嘗試過減法,但我相信代碼正在作業。我只是很難找到任何方法來防止單擊此功能清除和重置其他所有內容。
此代碼提取的所有資訊都來自文本框或數字框,預設值至少為“0”。我附上了一張我正在做的事情的照片。“健康:和”頭部其他“的代碼將添加并在“玩家最大健康”中顯示結果,并且沒有問題。(我在每個框上都有一個單獨的腳本,以防止輸入除數字之外的任何內容防止問題)。
All I am trying to do is is to take the 4 lines that says "Caroh's", add them up, then take the 4 lines that says "Passive Aura" and subtract those then add that to what ever number is in the aura box. I also just realized I need to add the Aura text box to the script to be added but will get to that later.
Any help is greatly appreciated, Javascript is not my strong point. Thanks

OK, I have changed the code to this way and am still not sure. It is as if everything is being submitted and all fields reset
<script>
function pta()
{
var pass1 = parseInt(document.getElementById("pass1").value);
var pass2 = parseInt(document.getElementById("pass2").value);
var pass3 = parseInt(document.getElementById("pass3").value);
var pass4 = parseInt(document.getElementById("pass4").value);
var car1 = parseInt(document.getElementById("car1").value);
var car2 = parseInt(document.getElementById("car2").value);
var car3 = parseInt(document.getElementById("car3").value);
var car4 = parseInt(document.getElementById("car4").value);
var aura = parseInt(document.getElementById("aura").value);
var ansD = document.getElementById("aura");
ansD.value = car1 car2 car3 car4 - pass1 - pass2 - pass3 - pass4 aura;
}
uj5u.com熱心網友回復:
我認為這里的問題是你使用了一個環繞<form>標簽,如果你只在客戶端執行 javascript 計算而不將資料發回服務器,你可以省略它。
放置form標簽后,表單內任何內容的默認行為都是如此,即使您有默認行為仍將觸發并提交清除所有欄位的表單。<button>submitonclick
如果您想保持<form>原位并且不希望此特定按鈕提交表單,最簡單的解決方法是添加type="button"例如
<button type="button" onclick="pta()">Click on your turn</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/451719.html
