使用 read-only 属性设置输入字段的默认值

Set default value of input field with read-only attribute

我有一个将持久数据发布到 SQL 数据库的表单。在我的表单中,我有一个输入字段,该字段最初设置为 read-only,除非用户选择一个选项:

我的问题是,如果用户选择不在输入字段中输入值,我会收到此错误:

我尝试在 javascript 中设置输入的默认值,但我收到同样的错误。

$("#lejeperiode").val(" ");

向数据库中插入空字符串或null 无关紧要。 这是我的输入字段:

<input id="periodeInput" class="form-control" type="text" name="lejeperiode" value="@(Request.Form["lejeperiode"] != null ? Request.Form["lejeperiode"].ToString() : "")"/>

修改您的代码以检查字符串是否为空,如果是,“”将是您的默认值,否则 POST 值。

cmd.Parameters.AddWithValue("@lejeperiode", string.IsNullOrEmpty(Request.Form["lejeperiode"]) ? "" : Request.Form["lejeperiode"].ToString())