Insert null value into a text input

假如想象 提交于 2020-12-30 07:34:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!