![](/img/trans.png)
[英]Javascript document.getElementById("id").value returning null instead of empty string when the element is an empty text box
[英]How can I give an empty material UI text field a value of null, when submitting the form, instead of an empty string?
我的問題是文本字段的默認值為零,但是當我提交表單時,文本字段的值是一個空字符串,我不希望這樣。 我希望該值最后為零。 我該如何實施?
我的 JSON 的一部分:
"basicData": {
"name": ""
"partner": ""
"riskFactor": ""
}
但我想要的是這個:
"basicData": {
"name": null
"partner": null
"riskFactor": null
}
也許有更有效的方法,但這是我想到的第一個解決方案。 我可以建議您在表單提交操作之前將空字符串轉換為 null。
Object.keys(YourData.basicData).forEach(key => {
if(YourData.basicData[key] === ""){
YourData.basicData[key] = null;
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.