jQuery code - IE8 problem
I have this code that changes input fields name attribute value, works great in all major browsers except IE8, IE7 and IE6. For IE the name just stay name="participant_name[{i}]"
, should be name="participant_name[1]"
.
var i = 1;
var input_name = null;
$('.dubl input').each(function () {
var new_input_name = $(this).attr('name').substring(0, $(this).attr('name').indexOf('['));
if (input_name !== new_input_name) {
input_name = new_input_开发者_JAVA技巧name;
i = 1;
}
$(this).attr('name', input_name + '[' + i + ']');
i++;
});
HTML
<input type="text" name="participant_name[{i}]">
It is in fact changing the attribute name
correctly in IE as well. However, I think the problem you may be experiencing is that when you submit the form, it isn't the updated field name
.
IE doesn't take into account that name
change and as such, when the form is sent, it is still using the same name
as it was when it was created. To fix this, you can either re-add it to the DOM the form/element when you change the name attribute.
http://jsfiddle.net/niklasvh/wbnhS/ you can test it there, the attribute does change or if you inspect the DOM in IE.
use .replace!
here's a quick fiddle example: http://jsfiddle.net/H5fkq/
If this is not the only place in your project where you need to do such replacements on the client, maybe it's worth looking at a templating library, e.g., jQuery Templates.
精彩评论