获取刚刚单击的按钮用户的ID [英] get ID of button user just clicked
问题描述
我已经检查了如何获取按钮用户ID点击?问这个问题.
I have checked How to get ID of button user just clicked? this question before asking.
我正在尝试获取按钮的ID.下面是代码.
I am trying to get the id of button. Below are the codes.
<form name="edit_posting1" id="edit_posting1" method="post" action="edit_posting_bk.php" role="form">
<center>
<input class="btn btn-home" type="submit" name="publish" id="publish" alt="Publish" value="Preview"/>
<input class="btn btn-home" type="submit" name="save" id="save" onclick="return confirm('Are you sure you want to Submit.')" alt="Save" value="Save"/>
<?php
if($disable == true){
echo '<input class="btn btn-home" type="submit" name="disable" id="disable" alt="Disable" value="Disable"/>';
}else{
echo '<input class="btn btn-home" type="submit" name="enable" id="enable" alt="Enable" value="Enable"/>';
}
?>
</center>
Ajax代码
<script type="text/javascript">
$("#edit_posting1").on("submit", function(e) {
e.preventDefault;
var btn = $('#publish');
//var el = 'publish';
var el = $(btn).attr('id');
alert(el);
})
</script>
根据我对Ajax代码的理解问题.有人可以建议我吗?
As per my understanding issue with Ajax code. Can somebody advise me on this?
谢谢
注意:我想要的是,如果用户单击启用/禁用或发布按钮,我会得到按钮的ID或名称.
Note: What i want is if user click on enable/disable or publish button i get the id or name of the button.
推荐答案
如果要获取在form submit
上按下的submit button
的ID,可以通过单击的listening
进行操作,保存为access that variable in form submit event handler
:
检查下面的代码段
If you want to get the id of pressed submit button
on form submit
you can do it by listening
which one was clicked, save it's id in variable
, and then access that variable in form submit event handler
:
Check the below snippet
var subm = "";
$('input[type="submit"]').click(function(e) {
subm = e.target.id;
});
$("#edit_posting1").on("submit", function(e) {
e.preventDefault();
var el = subm;
alert(el);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="edit_posting1" id="edit_posting1" method="post" action="edit_posting_bk.php" role="form">
<center>
<input class="btn btn-home" type="submit" name="publish" id="publish" alt="Publish" value="Preview" />
<input class="btn btn-home" type="submit" name="save" id="save" onclick="return confirm('Are you sure you want to Submit.')" alt="Save" value="Save" />
<?php if($disable==t rue){ echo '<input class="btn btn-home" type="submit" name="disable" id="disable" alt="Disable" value="Disable"/>'; }else{ echo '<input class="btn btn-home" type="submit" name="enable" id="enable" alt="Enable" value="Enable"/>'; }
?>
</center>
这篇关于获取刚刚单击的按钮用户的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!