这是我的表单代码,我想知道如何从数组按钮获取值。
$("#upload_form").submit(function(e) {
how to get the value of the Submit button I pressed..here ?
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="upload_form" name="upload_form" enctype="multipart/form-data" method="post" style="width: 95%">
<button type="submit" class="btn btn-success" id="btnUpload1" name="btnUpload" value="1"><span class="glyphicon glyphicon-cloud-upload"></span> Upload !</button>
<button type="submit" class="btn btn-success" id="btnUpload2" name="btnUpload" value="2"><span class="glyphicon glyphicon-cloud-upload"></span> Upload !</button>
</form>
谢谢
答案 0 :(得分:0)
您无法在表单的Submit事件处理程序中获取该按钮。
使用按钮单击事件处理程序来获取单击的按钮并保存按钮的信息。
以下是解决方法:
var submitor = undefined;
$('.btn').click(function(e) {
submitor = this;
})
$("#upload_form").submit(function(e) {
var val = $(submitor).val();
alert(val);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="upload_form" name="upload_form" enctype="multipart/form-data" method="post" style="width: 95%">
<button type="submit" class="btn btn-success" id="btnUpload1" name="btnUpload" value="1"><span class="glyphicon glyphicon-cloud-upload"></span> Upload !</button>
<button type="submit" class="btn btn-success" id="btnUpload2" name="btnUpload" value="2"><span class="glyphicon glyphicon-cloud-upload"></span> Upload !</button>
</form>