问题
I'm pretty sure it's not possible to do this but I'm still asking for it.
Is there a way for a user to insert a null (or an undefined) value into an html text input?
I need to distinguish between a null value and an empty string and I would like to allow the user to set this value into a single input.
For what I can see across the web, the standard solution to this problem is to match the text input with a checkbox that tell if the value is null or not. From my understanding, the limitation is linked to the fact that the textbox can hold an empty string but not a null value so that's why I think it's not possible to do exactly what I want.
回答1:
You can't tell a text input to have any kind of null
value. Empty checkboxes have a value of an empty string ''
by definition. The best way to do it, as you say, is to have a checkbox that toggles the disabled
property of the text input, which gives a similar semantic.
回答2:
Necromancing.
The accepted answer is kindof wrong - there is a way, sort of.
If you set the textbox state to disabled (for example with the chrome developer tools), then it won't submit the input box's value.
Hence, if you deserialize the form model from a post-request, the text's value will be NULL, unless your deserializer deliberately changes data.
来源:https://stackoverflow.com/questions/17240432/insert-null-value-into-a-text-input