[英]Add JavaScript onclick() to HTML form submit
I have the following script:我有以下脚本:
<script type= 'text/javascript'>
function displayPlayer() {
var input = document.getElementById("player_stuff").elements;
var position = input[0];
var player_id = input[1];
document.getElementById('disp_player').innerHTML = position + player_id
}
</script>
And a simple HTML form:还有一个简单的 HTML 表单:
<form id = 'player_stuff' onsubmit = 'displayPlayer()'>
Player's Position:<br>
<input type="radio" name="position" value="p1" checked>Position One
<input type="radio" name="position" value="p2" checked>Position Two
<input type="radio" name="position" value="p3" checked>Position Three
<input type="radio" name="position" value="p4" checked>Positin Four
<br/>
Add by Player ID:<br>
<input type='text' name='player_id'>
<input type="submit" value="Submit Player" id='smit' >
</form>
<div id = 'roster'>
<h3>Current Roster</h3>
<p id= 'disp_player'></p>
</div>
The idea is that when I click on the Submit Player button, it will trigger the displayPlayer() function and add the position name and player ID to the <p id= 'disp_player'></p>
tag.这个想法是当我点击提交播放器按钮时,它会触发displayPlayer()函数并将位置名称和播放器 ID 添加到
<p id= 'disp_player'></p>
标签。
What am I doing wrong, since nothing is showing up?我做错了什么,因为什么都没有出现?
Use the onsubmit
event.使用
onsubmit
事件。
You can add the onsubmit
event to the form
:您可以将
onsubmit
事件添加到form
:
<form id = 'player_stuff' onsubmit="displayPlayer()">
And remove the onclick
from the submit button:并从提交按钮中删除
onclick
:
<input type="submit" value="Submit Player" id='smit'>
There is an error with the JavaScript code as well. JavaScript 代码也有错误。 Change:
改变:
document.getElementById('disp_player').innerHTML = position + player_id
to:到:
document.getElementById('disp_player').innerHTML = position.value + player_id.value;
You can avoid submitting altogether by using the <button type="button">
, and then you can bind on the onclick
event of the button
.您可以通过使用
<button type="button">
完全避免提交,然后您可以绑定button
的onclick
事件。 You can simplify things by avoiding the input list instead using querySelector
:您可以通过避免输入列表而不是使用
querySelector
来简化事情:
<form id = 'player_stuff'>
Player's Position:<br>
<input type="radio" name="position" value="p1" checked>Position One
<input type="radio" name="position" value="p2">Position Two
<input type="radio" name="position" value="p3">Position Three
<input type="radio" name="position" value="p4">Positin Four
<br/>
Add by Player ID:<br>
<input type='text' name='player_id'>
<input type="button" value="Submit Player" id='smit' >
</form>
<div id="disp_player"></div>
Then using querySelector
and querySelectorAll
然后使用
querySelector
和querySelectorAll
document.getElementById('smit').onclick = function () {
var checkedPosition =
document.querySelectorAll("#player_stuff [name='position']:checked");
var playerId =
document.querySelector("#player_stuff [name='player_id']");
var position = checkedPosition[0].value;
var player_id = playerId.value;
document.getElementById('disp_player').innerHTML =
position + " " + player_id;
}
You have to stop the form's default action, which is to submit the form.您必须停止表单的默认操作,即提交表单。 Since you didn't declare the method in the form, like:
由于您没有在表单中声明方法,例如:
<form id = 'player_stuff' action="#" method="get">
the form will default to the GET method, as you can see when you submit the form.表单将默认为GET方法,正如您在提交表单时所看到的。 Your URL will change to "yourDomain.com/page.html?position=p4&player_id=1"
您的网址将更改为“yourDomain.com/page.html?position=p4&player_id=1”
To prevent the default action of the form, add this after your function:要防止表单的默认操作,请在您的函数后添加以下内容:
document.getElementById("player_stuff").addEventListener("click", function(event) {
event.preventDefault();
});
This will continue to run your function, while stopping the default function of the form element.这将继续运行您的函数,同时停止表单元素的默认函数。 Notice you will no longer see the URL with "?position=p4&player_id=1" added to the end, assuming you reloaded your page with just "yourDomain.com/page.html"
请注意,假设您仅使用“yourDomain.com/page.html”重新加载页面,您将不再看到末尾添加了“?position=p4&player_id=1”的 URL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.