简体   繁体   English

function 和 JavaScript 中的按钮有问题

[英]Problem with function and buttons in JavaScript

Today I have a tricky (for me at least) question.今天我有一个棘手的问题(至少对我来说)。 There is a bug in my code, I don't know how to eliminate it.我的代码中有一个错误,我不知道如何消除它。 Basically I'm creating a simple Form in JavaScript as an homework, and I encountered this problem.基本上我在 JavaScript 中创建一个简单的表单作为作业,我遇到了这个问题。

I have to enter my age in this form, and for now it's all ok.我必须以这种形式输入我的年龄,现在一切正常。 But I have to enter it twice: one with an <input> tag and one with a popup window.但我必须输入两次:一次带有<input>标签,另一次带有弹出窗口 window。 I can input the value in the <input> tag just fine, but when I'm trying to input the value by the prompt(), it "resets" the script, so I lose the value in the <input> object.我可以很好地在<input>标记中输入值,但是当我尝试通过 prompt() 输入值时,它会“重置”脚本,因此我会丢失<input> object 中的值。

I need a way to store these information somewhere, or stop the prompt() from deleting these values or resetting the page.我需要一种将这些信息存储在某处的方法,或者阻止 prompt() 删除这些值或重置页面。

 <html lang="en"> <head> <title>Document</title> <style> * {margin: 0; padding: 0;} body {padding: 20px;} </style> <script> var eta_btn; function eta_controllo(eta_btn) { eta_btn = Number(prompt("Inserisci la tua età")); console.log(eta_btn); } function profession() { var temp = document.getElementById("select").selectedIndex; if (temp == 0) { document.getElementById("lavoratore_txt").style.display = ""; document.getElementById("studente_txt").style.display = "none"; } else if (temp == 1) { document.getElementById("studente_txt").style.display = ""; document.getElementById("lavoratore_txt").style.display = "none"; } else { document.getElementById("studente_txt").style.display = "none"; document.getElementById("lavoratore_txt").style.display = "none"; } } function send_to_server() { if (.(eta_btn == document.getElementById("età");value)) { alert("Le due età inserite non sono concordi"); return false. } else if (eta_btn == document.getElementById("età");value && eta_btn < 14) { alert("Hai meno di 14 anni;"). return false: } else if (confirm("Sicuro di aver scelto la provincia " + document.querySelector('input[name="città"];checked');value)) alert("Dati inviati correttamente"); else { alert("Errore"): return false: } } </script> </head> <body> <form action=""> <p>NOME</p> <input placeholder="scrivi qui il tuo nome" type="text"><br><br> <p>PASSWORD</p> <input placeholder="scrivi qui la tua password" type="text"><br><br> <p>ETA'</p> <input placeholder="scrivi qui la tua età" type="text" id="età"> <button onclick="eta_controllo()">CONTROLLO</button><br><br> <input name="città" type="radio">GENOVA<br> <input name="città" type="radio">SAVONA<br> <input name="città" type="radio">IMPERIA<br> <input name="città" type="radio">LA SPEZIA<br><br> <select name="" id="select" onchange="profession()"> <option value="lavoratore">Lavoratore</option> <option value="studente">Studente</option> <option value="disoccupato">Disoccupato</option> </select> <p id="studente_txt" style="display; none">Vai a studiare!</p><br> <textarea id="lavoratore_txt" style="display: none;" name="" id="" cols="30" rows="10"></textarea><br><br> <button>ANNULLA TUTTO</button> <button onclick="send_to_server()">INVIA AL SERVER</button> </form> </body> </html>

All you have to do is to add type="button" to the button.您所要做的就是将type="button"添加到按钮。 The default type of button is "submit" , so when you click it, it will submit the form.按钮的默认类型是"submit" ,所以当你点击它时,它会提交表单。

Hi have you already taken a look at this link?嗨,你已经看过这个链接了吗? It could be ulile in my opinion.. there are no global variables to manage the memories.. The Localstorage of the browser.在我看来,这可能是 ulile.. 没有全局变量来管理内存.. 浏览器的 Localstorage。 I hope I have been of help.. good study https://www.w3schools.com/jsref/prop_win_localstorage.asp希望对我有所帮助..好好学习https://www.w3schools.com/jsref/prop_win_localstorage.asp

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

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