表单onSubmit确定哪个提交按钮被按下 [英] Form onSubmit determine which submit button was pressed

查看:132
本文介绍了表单onSubmit确定哪个提交按钮被按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个提交按钮和一些代码的表单:

HTML:

 < input type =submitname =savevalue =Save/> 
< input type =submitname =saveAndAddvalue =保存并添加另一个/>

Javascript:

  form.onSubmit = function(evnt){
//做一些异步的东西,稍后将提交表单
return false;
}

当然这两个提交按钮完成不同的事情。有没有办法找到 onSubmit 哪个按钮被按下,所以稍后我可以通过来提交thatButton.click()

理想情况下,我不想修改按钮的代码,只需要一个具有此行为的纯javascript插件。



我知道FF有 evnt.explicitOriginalTarget 但我找不到其他浏览器的任何内容。



但是可以 > do是为每个提交添加点击处理程序,它将通知提交处理程序关于哪个被点击。



下面是一个完整的示例(为简洁起见,使用jQuery)

 < html> 
< head>
< title>测试页< /标题>
< script src =http://code.jquery.com/jquery-latest.js>< / script>
< script type =text / javascript>

jQuery(function($){
var submitActor = null;
var $ form = $('#test');
var $ submitActors = $ form .find('input [type = submit]');

$ form.submit(function(event){
if(null === submitActor){
//如果没有显式地点击演员,浏览器将
//自动选择第一个源代码
//所以我们在这里做同样的操作
submitActor = $ submitActors [0];


console.log(submitActor.name);
// alert(submitActor.name);

return false;
}) ;

$ submitActors.click(function(event){
submitActor = this;
});
});

< / script>
< / head>

< body>

< form id =test>

< input type =text/>

< input type =submitname =savevalue =Save/>
< input type =submitname =saveAndAddvalue =保存并添加另一个/>

< / form>

< / body>
< / html>


I have a form with two submit buttons and some code:

HTML:

<input type="submit" name="save" value="Save" />
<input type="submit" name="saveAndAdd" value="Save and add another" />

Javascript:

form.onSubmit = function(evnt) {
    // Do some asyncrhnous stuff, that will later on submit the form
    return false;
}

Of course the two submit buttons accomplish different things. Is there a way to find out in onSubmit which button was pressed, so later I could submit by doing thatButton.click()?

Ideally I would like to not modify the code for the buttons, just have a pure-javascript addon that has this behavior.

I know that FF has evnt.explicitOriginalTarget but I can't find anything for other browsers.

解决方案

Not in the submit event handler itself, no.

But what you can do is add click handlers to each submit which will inform the submit handler as to which was clicked.

Here's a full example (using jQuery for brevity)

<html>
<head>
  <title>Test Page</title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript">

  jQuery(function($) {
      var submitActor = null;
      var $form = $('#test');
      var $submitActors = $form.find('input[type=submit]');

      $form.submit(function(event) {
          if (null === submitActor) {
              // If no actor is explicitly clicked, the browser will
              // automatically choose the first in source-order
              // so we do the same here
              submitActor = $submitActors[0];
          }

          console.log(submitActor.name);
          // alert(submitActor.name);

          return false;
      });

      $submitActors.click(function(event) {
          submitActor = this;
      });
  });

  </script>
</head>

<body>

  <form id="test">

    <input type="text" />

    <input type="submit" name="save" value="Save" />
    <input type="submit" name="saveAndAdd" value="Save and add another" />

  </form>

</body>
</html>

这篇关于表单onSubmit确定哪个提交按钮被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆