The function called at the onclick attribute for the input does not exist and it will never work.(在输入的onclick属性上调用的函数不存在,它将永远无法使用。)
What you did was almost correct.(你所做的几乎是正确的。) You added the click event for the submit button that calls a function.(您为调用函数的提交按钮添加了click事件。) That function will always (i think so) have one parameter (Event type) and not those added by you.(该函数将始终(我认为是)具有一个参数(事件类型),而不是您添加的参数。)
In order to retrieve the values for the inputs, you need to retrieve them in that function.(为了检索输入的值,您需要在该函数中检索它们。)
Check the snippet below.(检查下面的代码段。)
var submitButton = document.getElementById("btnsubmit"); submitButton.onclick = function submit() { confirm("Are you sure you want to submit the form?"); if(confirm) { var fname = document.getElementById('txtfirstName').value; var sname = document.getElementById('txtsecondName').value; var uname = document.getElementById('txtuserName').value; var email = document.getElementById('txtemail').value; var password = document.getElementById('txtpassword').value; alert("Below is the data entered by you:" + "\n" + fname + "\n" + sname + "\n" + uname + "\n" + email + "\n" + password ) } }
<form > First Name: <input type="text" id="txtfirstName" /> Second Name: <input type="text" id="txtsecondName" /> User Name: <input type="text" id="txtuserName" /> Email: <input type="text" id="txtemail" /> Password: <input type="password" id="txtpassword" /> <input type="button" id= "btnsubmit" value="SUBMIT" /> </form>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…