javascript 用多个提交按钮识别点击提交按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15495820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identify the value of clicked submit button with multiple submit buttons
提问by Harshana
I have situation where i need to track which submit button click in order to do set variables according to that. Below is the test code,
我有一种情况,我需要跟踪哪个提交按钮点击,以便根据那个设置变量。下面是测试代码,
<script>
function submitForm(form) {
alert(document.getElementById('sb').value);
if (document.getElementById('sb').value=="One") {
//Do something
}
return true;
}
</script>
<form action="" method="get" onsubmit="return submitForm(this);">
<input type="submit" name="sb" value="One">
<input type="submit" name="sb" value="Two">
<input type="submit" name="sb" value="Three">
</form>
The alert always shows One even if i click button Two or Three. But the url change with clickable parameter. How to alert the value which is in the clickable submit button?
即使我单击按钮二或三,警报也总是显示一。但是 url 随可点击参数而变化。如何提醒可点击提交按钮中的值?
Note: I want a solution with out JQuery
注意:我想要一个没有 JQuery 的解决方案
EDIT: I change the code bit which the onsubmit call the submitForm(this); The problem is even use document.forms[0].sb.value its undefined because document.forms[0].sb return a node list of all submit buttons as its same as with document.getElementById('sb')
编辑:我更改了 onsubmit 调用 submitForm(this) 的代码位;问题甚至是使用 document.forms[0].sb.value 未定义,因为 document.forms[0].sb 返回所有提交按钮的节点列表,与 document.getElementById('sb') 相同
采纳答案by Piotr Kowalski
Continuing the answer above:
继续上面的回答:
<script>
function m(value) {
alert(value);
}
</script>
<input type="button" value="One" onClick="m(this.value)">
<input type="button" value="Two" onClick="m(this.value)">
<input type="button" value="Three" onClick="m(this.value)">
You can of course see what's the id:
你当然可以看到id是什么:
<input type="button" id='myId' value="Three" onClick="m(this.id)">
回答by A human being
I would suggest you to use buttons, instead of multiple submit buttons. In the onclick attribute of the buttons, submit the form using javascript.
我建议您使用按钮,而不是多个提交按钮。在按钮的 onclick 属性中,使用 javascript 提交表单。
回答by Bangarraju
You can try like this,
你可以这样试试
<form>
<input class="myButton" type="submit" name="sb" value="One">
<input class="myButton" type="submit" name="sb" value="Two">
<input class="myButton" type="submit" name="sb" value="Three">
</form>
<script type="text/javascript">
$(".myButton").on('click', function() {
alert($(this).val());
});
</script>
回答by brehma
I'm a bit new to javascript; please forgive me if I'm wrong on this. Wouldn't it make a difference if your if statement had a 3rd =
sign?
我对 javascript 有点陌生;如果我错了,请原谅我。如果你的 if 语句有第三个=
标志,那不会有什么不同吗?
Should it be:
应该是:
if (document.getElementById('sb').value === "One") {
//Do something
}
return true;
回答by G. Allen Morris III
Here is what I think is a simpler solution to this problem. It does not require any extra events.
这是我认为解决这个问题的更简单的方法。它不需要任何额外的事件。
<script>
function submitForm(form) {
console.log(document.activeElement.value);
if (document.activeElement.value == 'One') {
console.log("Have one.");
return false;
}
return true;
}
</script>
<form action="" method="get" onsubmit="return submitForm(this);">
<input type="submit" name="sb" value="One">
<input type="submit" name="sb" value="Two">
<input type="submit" name="sb" value="Three">
</form>
What I would like an answer to is how the form is getting the query set to "?sb={value}".
我想要的答案是表单如何将查询设置为“?sb={value}”。
回答by Francois Borgies
you can try with jquery something like :
您可以尝试使用 jquery 之类的东西:
$(":submit").live('click', function() {
alert($(this).val());
})