以多种形式获取提交按钮名称或值

时间:2012-12-17 18:31:52

标签: jquery forms

我知道这里有很多帖子,但我找不到有用的东西没有运气。

我在页面上有多个表单,我希望能够使用每个表单中的提交按钮逐个提交。目前这对我有用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    <!--
    $(document).ready(function() {
        $(".do_submit").click(function() {
            var element = $(this);
            var Id = element.attr("id");
            var val = $("#val" + Id).val();
            var anotherval = $("#anotherval" + Id).val();
            alert("Id " + Id + " val " + val + " anotherval " + anotheval);
                    // How do I get the name of the value of whatever submit button was clicked
        });
    }); 
    -->
</script>   
</head>
<body>
<form action="" method="post" name="form1" />
  <input type="hidden" name="val" id="val1" value="a" />   
  <input type="hidden" name="anotherval" id="anotherval1" value="b" />  
  <input type="submit" name="SavePlantInfo" class="do_submit" id="1" value="Save" />
  <input type="submit" name="SavePlantInfoAndEmail" class="do_submit" id="1" value="Save And Email" />                                   
</form>

<form action="" method="post" name="form2" />
  <input type="hidden" name="val" id="val2" value="a" />   
  <input type="hidden" name="anotherval" id="anotherval2" value="b" />  
  <input type="submit" name="Save" class="do_submit" id="2" value="Save" />
  <input type="submit" name="SaveAndEmail" class="do_submit" id="2" value="Save And Email" />                                   
</form>
</body>
</html>

我无法弄清楚的是如何知道点击了哪两个提交按钮。我希望能够获得名称(首选)或点击任何提交按钮的值。

TIA!

3 个答案:

答案 0 :(得分:3)

你有没有尝试过:

$(this).attr("name");

答案 1 :(得分:0)

尝试这样;

 $(".do_submit").click(function() {
          var ele=$(this).val();
    if(ele=='Save')
    {
    /// your code
    }
    else
    {
    // save and email
    }
    });​

答案 2 :(得分:0)

$(".do_submit").click(function() {
    var buttonName = this.name; // OR $(this).attr('name')
    // your code
});