我有一个将持久数据发布到 SQL 数据库的表单。在我的表单中,我有一个输入字段,最初设置为只读,除非用户选择一个选项:
我的问题是,如果用户选择不在输入字段中输入值,我会收到此错误:
我尝试在 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())
关于javascript - 设置具有只读属性的输入字段的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46071924/