admin管理员组文章数量:1289402
I'm parsing a JSON response via $.ajax()
and building a form from this object's values. The script I've written is long, but here's what it's doing:
Dynamically creating:
~ a form element,
~ a fieldset element,
~ a button element,
~ 20 or so text inputs and label elementsAppending the inputs and labels to the fieldset
Appending the button to the fieldset
Appending the fieldset to the form
- Appending the form to an element in the existing DOM.
Everything is working in all browsers except one small snippet in IE. I've narrowed it down to the following piece of code. (doc
is a variable containing document
)
fieldset.append(
$(doc.createElement('button'))
.addClass('ui-button')
.attr('type', 'submit')
.html('Re-Rate')
.button()
);
This is step 3 from above. It creates a button element, adds a class, sets the type
attribute to submit
, gives it some text, and then appends it to the fieldset. IE fails with the error "Object doesn't support this action"
If I ment out the .attr()
line like this:
fieldset.append(
$(doc.createElement('button'))
.addClass('ui-button')
//.attr('type', 'submit')
.html('Re-Rate')
.button()
);
Everything works as expected.
If you're wondering, the .button()
method is jQuery UI
I'm parsing a JSON response via $.ajax()
and building a form from this object's values. The script I've written is long, but here's what it's doing:
Dynamically creating:
~ a form element,
~ a fieldset element,
~ a button element,
~ 20 or so text inputs and label elementsAppending the inputs and labels to the fieldset
Appending the button to the fieldset
Appending the fieldset to the form
- Appending the form to an element in the existing DOM.
Everything is working in all browsers except one small snippet in IE. I've narrowed it down to the following piece of code. (doc
is a variable containing document
)
fieldset.append(
$(doc.createElement('button'))
.addClass('ui-button')
.attr('type', 'submit')
.html('Re-Rate')
.button()
);
This is step 3 from above. It creates a button element, adds a class, sets the type
attribute to submit
, gives it some text, and then appends it to the fieldset. IE fails with the error "Object doesn't support this action"
If I ment out the .attr()
line like this:
fieldset.append(
$(doc.createElement('button'))
.addClass('ui-button')
//.attr('type', 'submit')
.html('Re-Rate')
.button()
);
Everything works as expected.
If you're wondering, the .button()
method is jQuery UI
-
What happens if you
doc.createElement()
it first, assign it the type in plain JS, and then turn it into a jQuery object? Just to find out which step the problem is in? – Pekka Commented Oct 14, 2010 at 19:35 - 1 You may want to think about using a jQuery templating engine - example – John Strickler Commented Oct 14, 2010 at 19:41
- Good idea John. Pekka, per Nick's answer it seems to be jQuery-By-Design. – Stephen Commented Oct 14, 2010 at 19:44
2 Answers
Reset to default 10jQuery doesn't allow you to change the type
of an <input>
or <button>
element.
The reason for this is consistency, and IE doesn't allow you to change the type
once it's been inserted into the DOM.
jQuery won't let you modify the type
attribute on an existing button element because IE throws an error when you try to do so.
However, you can try something like this (using jQuery 1.4's more concise element creation syntax):
fieldset.append(
$('<button>', {'type': 'submit', 'class': 'ui-button', 'html': 'Re-Rate'}).button()
);
本文标签:
版权声明:本文标题:javascript - jQuery .attr('type', 'submit') on a button element giving me a strange error in IE7 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741442404a2378995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论