Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

javascript - 单击按钮无法将值传递给函数(Unable to pass values to a function on click of a button)

I am a beginner in javascript and was just trying to implement a simple form.(我是javascript的初学者,只是想实现一种简单的形式。)

However, I am unable to submit it since the values that are entered in the textbox are not getting passed further to the function that I call on clicking the submit button.(但是,我无法提交它,因为在文本框中输入的值没有进一步传递给我单击“提交”按钮时调用的函数。) I have seen similar questions and even followed the answers still for some reason I only get undefined instead of the data passed.(我曾经看到过类似的问题,甚至由于某些原因仍然遵循答案,但我只能得到未定义的信息,而不是传递的数据。) Below is the code:(下面是代码:) Html:(HTML:) <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" onclick="submit("name","surname","user","mail","word")" /> </form> <script> var fname = ""; var sname = ""; var uname = ""; var email = ""; var password = ""; var submitButton = document.getElementById("btnsubmit"); submitButton.onclick = function submit(fname,sname,uname,email,password) { confirm("Are you sure you want to submit the form?"); if(confirm) { alert("Below is the data entered by you:" + " " + fname + " " + sname + " " + uname + " " + email + " " + password ) } } </script>   ask by Learner.123 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...