Html 将空值插入文本输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17240432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:20:32  来源:igfitidea点击:

Insert null value into a text input

htmlinputtextboxnullstring

提问by The_Black_Smurf

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?

用户有没有办法将空(或未定义)值插入到 html 文本输入中?

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.

对于我在网上看到的,这个问题的标准解决方案是将文本输入与一个复选框匹配,该复选框判断该值是否为空。根据我的理解,限制与文本框可以包含空字符串但不能包含空值的事实有关,这就是为什么我认为不可能完全按照我的意愿去做。

回答by lonesomeday

You can't tell a text input to have any kind of nullvalue. 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 disabledproperty of the text input, which gives a similar semantic.

您不能告诉文本输入具有任何类型的null值。''根据定义,空复选框具有空字符串值。正如您所说,最好的方法是使用一个复选框来切换disabled文本输入的属性,这提供了类似的语义。

回答by Stefan Steiger

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.

死灵法术。
接受的答案有点错误 - 有一种方法。
如果您将文本框状态设置为禁用(例如使用 chrome 开发人员工具),则它不会提交输入框的值。
因此,如果您从后请求反序列化表单模型,则文本的值将为 NULL,除非您的反序列化器故意更改数据。