javascript - 设置具有只读属性的输入字段的默认值

标签 javascript c# html forms default

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

enter image description here

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

enter image description here

我尝试在 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/

相关文章:

javascript - 如何使用正则表达式在 angularjs 上打印值?

javascript - Protractor :ElementArrayFinder 到通用数组

c# - 如何从视频文件的 "Media Created"列中提取日期?

c# - 有什么方法可以在不创建文件的情况下设置长 url 字符串吗?

html - 如何为网页背景显示 gif 全屏?

javascript - HTML 表格和数据表对齐

javascript - 在 React 类中声明并重用变量?

c# - 从模拟方法返回模拟

html - IE、溢出和消失的列表项

javascript - vue js 中的自定义图像 slider