问题:
I have a .submit()
event set up for form submission.我为表单提交设置了一个.submit()
事件。 I also have multiple forms on the page, but just one here for this example.我在页面上也有多个表单,但在此示例中只有一个表单。 I'd like to know which submit button was clicked without applying a .click()
event to each one.我想知道在没有对每个提交按钮应用.click()
事件的情况下点击了哪个提交按钮。
Here's the setup:这是设置:
<html>
<head>
<title>jQuery research: forms</title>
<script type='text/javascript' src='../jquery-1.5.2.min.js'></script>
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$('form[name="testform"]').submit( function(event){ process_form_submission(event); } );
});
function process_form_submission( event ) {
event.preventDefault();
//var target = $(event.target);
var me = event.currentTarget;
var data = me.data.value;
var which_button = '?'; // <-- this is what I want to know
alert( 'data: ' + data + ', button: ' + which_button );
}
</script>
</head>
<body>
<h2>Here's my form:</h2>
<form action='nothing' method='post' name='testform'>
<input type='hidden' name='data' value='blahdatayadda' />
<input type='submit' name='name1' value='value1' />
<input type='submit' name='name2' value='value2' />
</form>
</body>
</html>
Live example on jsfiddle jsfiddle 上的实时示例
Besides applying a .click() event on each button, is there a way to determine which submit button was clicked?除了在每个按钮上应用 .click() 事件之外,有没有办法确定点击了哪个提交按钮?
解决方案:
参考一: https://en.stackoom.com/question/O0Ts参考二: https://stackoom.com/question/O0Ts