سؤال

I have something like this in my web forms:

<input type="hidden" name="myField" value="defaultValue" />

Later on, in some Javascript, I am overwriting the default value before I submit the page to the server.

var formField = document.getElementsByName("myField")[0];
formField.setAttribute("value", "myNewValue");
var form = document.getElementById("myForm"); 
form.submit();

All browsers (I tested this code over years in IE5-IE8, Firefox, Opera, Chrome, Safari, ...) are sending "myNewValue" to the server. Except IE9: It sends "defaultValue". What's going on here? Am I missing something?

If I remove the "value" attribute from the field, it also works in IE9. It also works in IE9 if I switch to IE8 rendering mode.

Is this a bug or is IE9 more standards compliant than the other browsers?

هل كانت مفيدة؟

المحلول

David Dorward appears to be correct as regards to HTML4, but this seems to be backward-compatibility breaking behaviour, so I would expect HTML5 to describe what browsers actually do. And it seems to. It says this: (my emphasis)

The value content attribute gives the default value of the input element. When the value content attribute is added, set, or removed, if the control's dirty value flag is false, the user agent must set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise, and then run the current value sanitization algorithm, if one is defined.

See http://dev.w3.org/html5/spec/the-input-element.html#attr-input-value

Your scenario doesn't describe anything that would set the dirty value flag prior to the setAttribute call, so I believe that the setAttribute call should set the value property and the browser should send myNewValue not defaultValue.

So yes, I think it is a IE9 Beta bug.

نصائح أخرى

The value attribute is defined as setting the initial value.

Once the form has been loaded, the field will have it's actual value set to it's initial value.

As far as I know, there is nothing that says the current value should change if the initial value changes after the form has loaded, so I would suspect that this is a bug fix and not a new bug.

Use the value property instead of the setAttribute method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top