javascript - What's should setting an HTML textbox value to null do? -
We have a JSON response in which null values can be included (such as {myValue: null}) - we get a text box On a form: (In fact, we use JQuery, but this is equal to
var nullString = null; document.getElementById ('myInput'). Value = nullString; If we assign this value to an HTML textbox value, the behavior is dependent on browser-based:
-
Both firefox and chrome are empty Lesson , And when you get 'value' back you get empty.
-
IE puts the string 'null' in the text box, and when you read 'value' back , You get the string "tap" (
(here it is: if someone wants to try it)
what browser is Here's the right thing, or is it an undefined behavior? And what do we do (my value == faucet?): My vine is stuff?
What is happening in Astw, being Strac empty object, such as NullktoString () in an alert () message box, browser usually explain it objects rather than string, so the result will be different. And obviously, browsers (especially IE) make different interpretations and stripes.
What is the price, when a string is expected, then there is an argument to return empty, whatever is returning this value, it may be possible to provide a wrong value for easy conditional testing. , While still indicating that the result is empty, e.g. Undefined DOM methods that return storage objects do the same. Also, while an empty string is literally ('') is a false value, there is no empty string object (new string ('')); And later can be accidentally created with processing in the east. Therefore, by rejecting a valid string by mistake, it is to return a string or string object in one case in the "safe" way to prevent the consumer code, and the other in the null.
Comments
Post a Comment