我可以确定javascript中使用了哪个“提交"按钮吗? [英] Can I determine which Submit button was used in javascript?
问题描述
我有一个非常简单的表单,其中包含一个名称字段和两个提交按钮:更改"和删除".提交表单时,我需要在JavaScript中进行一些表单验证,因此我需要知道单击了哪个按钮.如果用户按下回车键,则"change"值就是使它进入服务器的值.所以说真的,我只需要知道是否单击了删除"按钮即可.
I have a very simple form with a name field and two submit buttons: 'change' and 'delete'. I need to do some form validation in javascript when the form is submitted so I need to know which button was clicked. If the user hits the enter key, the 'change' value is the one that makes it to the server. So really, I just need to know if the 'delete' button was clicked or not.
我可以确定单击了哪个按钮吗?还是我需要将删除"按钮从提交按钮更改为常规按钮,并捕获其onclick事件以提交表单?
Can I determine which button was clicked? Or do I need to change the 'delete' button from a submit to a regular button and catch its onclick event to submit the form?
表单如下:
<form action="update.php" method="post" onsubmit="return checkForm(this);">
<input type="text" name="tagName" size="30" value="name goes here" />
<input type="hidden" name="tagID" value="1" />
<input type="submit" name="submit" value="Change" />
<input type="submit" name="submit" value="Delete" />
</form>
在 checkForm()
函数中, form ["submit"]
是节点列表,而不是我可以获取其值的单个元素.
In the checkForm()
function, form["submit"]
is a node list, not a single element I can grab the value of.
推荐答案
您还可以通过多种不同方式使用 onclick
事件来解决该问题.
You could also use the onclick
event in a number of different ways to address the problem.
例如:
<input type="submit" name="submit" value="Delete"
onclick="return TryingToDelete();" />
在JavaScript的 TryingToDelete()
函数中,执行所需的操作,然后如果不想删除继续,请
In the TryingToDelete()
function in JavaScript, do what you want, then return false
if do not want the delete to proceed.
这篇关于我可以确定javascript中使用了哪个“提交"按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!