最新的 jQuery 1.4 现在支持“实时”提交事件——这意味着您不必将单个处理程序附加到所有表单。Paul Irish 在这里给出了一个很好的例子,涵盖了你所问的内容:
http://jquery14.com/day-05/jquery-1-4-hawtness-1-with-paul-irish
这是我自己的看法:
jQuery(document).ready(function() {
var pageForms = jQuery('form');
pageForms.find('input[type="submit"]').live('click', function(event) {
var submitButton = this;
var parentForm = jQuery(jQuery(this).parents('form')[0]);
parentForm.data('submit-button',submitButton);
});
pageForms.live('submit', function(event) {
// Prevent form-submission. You can do this conditionally later, of course
event.preventDefault();
// The form that was submitted
var theForm = jQuery(this);
// Detect which submit button was pushed
var submitButton = theForm.data('submit-button');
console.log('submitButton = ',submitButton.value);
});
});
HTML:
<form>
<input type="submit" value="submit form 1" />
</form>
<form>
<input type="submit" value="submit form 2" />
<input type="submit" value="submit form 3" />
</form>
http://jsbin.com/equho3/6/edit
编辑-对不起,我在这里发布了一个与您的链接中的示例相匹配的示例!我现在提供了一个跨浏览器的解决方案。