确认三个提交按钮,其值为确认消息

时间:2013-11-01 10:23:01

标签: javascript html forms

这里我不想从提交按钮传递参数。任何人都可以帮我提醒相应的确认信息

return confirm("Are you sure to Approve/Reject/Delete ?");

我的HTML代码

<form name="pendingrequest" id="pendingrequest" action="formhandler.php" method="POST" onsubmit="return confirmation(this);">
            <table >
                <tr>
                    <td><input type="submit" name="action" value="Approve"></td>
                    <td><input type="submit" name="action" value="Reject"></td>
                    <td><input type="submit" name="action" value="Delete"></td>
                </tr>
            </table> 
</form>

例如,如果有人点击批准,我想要这个:

return confirm("Are you sure to Approve?");

1 个答案:

答案 0 :(得分:1)

如果您使用onsubmit

,则没有跨浏览器解决方案

执行此操作的唯一方法是将事件绑定到按钮而不是提交表单:

<!DOCTYPE html>
<html>
 <head>
  <script>
   function init()
   {
    document.getElementById("pendingrequest").onsubmit = function(e)
    {
     console.log(e.target.value);
     console.info(e.srcElement);
     return false;
    }

    var submits = document.getElementsByClassName("submit-test");
    for(var i=0;i<submits.length;i++)
    {
     submits[i].onclick = function(e){
      var value = this.value;
      return confirm("Are you sure to "+value+" ?");
     }
    }
   }
  </script>
 </head>
 <body onload="init()">

  <form name="pendingrequest" id="pendingrequest" action="" method="POST" >
   <table >
    <tr>
     <td><input class="submit-test" type="submit" name="action" value="Reject"></td>
     <td><input class="submit-test" type="submit" name="action" value="Approve"></td>
     <td><input class="submit-test" type="submit" name="action" value="Delete"></td>
    </tr>
   </table> 
  </form>
 </body>
</html>