提交按钮不会触发IE上的jQuery绑定提交事件
我在IE上有问题(8,尚未在7上进行测试).我有一个非常简单的情况:我创建一个表单元素
I have a problem with IE (8, have not tested on 7). I have a very simple situation: I create a form element
var formNode = $('<form>');
我将一堆其他元素附加到表单(表内具有输入元素的字段集).然后,我将表单附加到DOM,然后绑定一个提交处理程序:
I attach a bunch of other elements to the form (fieldsets with input elements inside of tables). I then attach the form to the DOM, and then bind a submit handler:
formNode.bind('submit', function (ev) {
alert('submit of form!');
// do lots of stuff....
}
这可以在Mac上的Safari(是的,我是Mac电脑)上使用Safari,在Windows上的Safari和Firefox上也可以使用,但是当我使用IE 8时单击提交"按钮时,永远不会调用警报.尝试为/提交GET请求(我没有为表单设置方法或操作-也许有必要吗?).
This works with Safari on my Mac (yah, I'm a Mac guy), and on Safari and Firefox on Windows, but the alert is never called when I click the submit button when using IE 8. Instead, the browser tries to submit a GET request for / (I have not set either a Method or an Action for the form - perhaps that is necessary?).
在我进行调试时碰碰运气的任何见解将不胜感激.
Any insights would be greatly appreciated as I am making changes hit-or-miss in order to debug.
在Internet Explorer中,使用jQuery侦听来自表单元素的事件存在很多问题. jQuery论坛上有一系列有关以下内容的评论:
与IE相关的实时特定问题(提交") .当然,我知道您使用的是bind
而不是live
,但是所描述的许多问题都涉及尝试同时使用这两种方法.
There a variety of issues with listening for events from form elements with jQuery in Internet Explorer. There are a series of comments on the jQuery forum about:
IE-specific issues with live ('submit'). Granted I know you are using bind
and not live
but many of the described problems involve attempts at using both.
我评论了您的答案,询问您使用的是哪个版本的jQuery.如果您还没有这样做,可以这样做,我会升级到最新版本.
I commented on your answer asking which version of jQuery you are using. If you haven't already and can do so I'd upgrade to the latest release.
一些代码解决方案:
Bind the submit handler to each child of the body element
adapted from synertech1 answer in the aforementioned jQuery forum discussion.
var submitHandler = function() { ... };
$("body").children().each(function() {
$("form", this).bind("submit", submitHandler);
})
绑定到提交输入类型
我讨厌这个选项,因为form元素是绑定到的更合适的元素.
Bind to the submit input type
I hate this option because the form element is the more appropriate element to bind to.
$('form > input[type*="submit"]').bind('click', function(event) {
var form = $(this).parent;
form.submit();
event.preventDefault();
});