如何从表单提交事件中获取导致提交的按钮值

 

 

<!DOCTYPE html>
<html>
<head>
  <title>如何从表单提交事件中获取导致提交的按钮?</title>
  <meta charset="utf-8">
  
  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>  
</head>
<body>

<form class="layui-form" method="post" >

<button type="submit" class="layui-btn" value="0" lay-submit="" lay-filter="demo1">保存</button>
<button type="submit" class="layui-btn yllanse"  value="-1"  lay-submit="" lay-filter="demo1">预览</button>


</form>

<script type="text/javascript">
//给每个按钮增属性 $("form button[type=submit]") 我这个是按钮,如果你是<input type="submit" >请把$("form input[type=submit]")换成input的 

$("form button[type=submit]").click(function() { 
  $("button[type=submit]", $(this).parents("form")).removeAttr("clicked");
  $(this).attr("clicked", "true");
});


$(document).ready(function(){ 

  $('.layui-form').submit(function (event) {

    //$("button[type=submit][clicked=true]")我这是按钮形式,如果你是 <input type="submit" /> 请换成$("input[type=submit][clicked=true]").val()形式
    
    var val = $("button[type=submit][clicked=true]").val();
    console.log(val);
    return false;
  });
  

});

</script>

</body>
</html>

 

posted @ 2019-11-11 17:07  Abner3721  阅读(347)  评论(0编辑  收藏  举报