jQuery表单提交参数检查 [英] jQuery Form Submission Parameter Check
问题描述
我可以在$('#formid').submit(function() { });
调用中使用jQuery拦截/询问表单提交值吗?我无法检查页面中的值,因为需要检查的是特定的提交按钮.我已经找到了 http://geekswithblogs.net/renso/archive/2009/09/09/intercept-a-form-submit-with-jquery-and-prevent-or-allow.aspx 而且它可以在FF中使用,但不能在IE8中使用,因此很遗憾,这没有帮助.我也不想将提交代码附加到按钮上,因为我希望编写一些通用的代码,然后将它们插入整套表单中.
Can I intercept/interrogate the form submission values using jQuery in the $('#formid').submit(function() { });
call? I can't check the value from the page as it's the particular submit button pressed that I need to check. I've found this http://geekswithblogs.net/renso/archive/2009/09/09/intercept-a-form-submit-with-jquery-and-prevent-or-allow.aspx and it works in FF, but not in IE8, so that doesn't help, unfortunately. I also don't want to have to attach submit code to the buttons as I was hoping to make a generic bit of code I could plug into a whole set of forms.
欢呼
MH
推荐答案
最新的jQuery 1.4现在支持实时"提交事件-意味着您不必将单独的处理程序附加到所有表单上.保罗·爱尔兰(Paul Irish)在这里给出了一个很好的例子,涵盖了您的要求:
The latest jQuery 1.4 supports 'live' for submit events now -- meaning you don't have to attach individual handlers to all your forms. A nice example that covers what you've asked is given by Paul Irish here:
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
编辑-抱歉,我在这里发布了一个示例,该示例与您的链接中的示例相匹配!我现在提供了跨浏览器的解决方案.
EDIT - Sorry, I posted an example here that matched the one in your link! I've now provided a cross-browser solution.
这篇关于jQuery表单提交参数检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!