[英]How to show empty string when value is NULL in jQuery on .append() funtion
[英]How show Empty string in Textbox when Date is NULL
我有一個 DateTime,因為這是 C#:
public DateTime ShipmentDate { get; set; }
我在文本框中顯示值
這是在 cshml 中:
<div class="col-sm-2">
<label for="ShipmentDate">Shipment Date:</label><br />
<input type="text" name="ShipmentDate" id="ShipmentDate" disabled="disabled" />
</div>
這是在 JavaScript 中:
$('#ShipmentDate').val($.formatDate(PurchaseOrder.ShipmentDate));
當 ShipmentDate 像這樣可以為空時:
public DateTime? ShipmentDate { get; set; }
並且日期為空,它在文本框中顯示一些隨機日期為 1960/12/31
當 ShipmentDate 不可為空時,將日期顯示為:文本框中的 1/1/0001
在 C# 中,日期都是 Null,當我這樣做時,console.log 在控制台中顯示 null。 當 ShipmentDate 為 Null 時,如何在文本框中顯示空字符串。
由於PurchaseOrder.ShipmentDate
可以為空,因此處理這種情況是值得的,這樣$.formatDate(null)
就不會像注意到的那樣返回無效日期。
在 Javascript 中,如果變量為null
,則將其視為假值。 所以它只執行formatDate
如果ship
不是許多虛假值之一,包括null
。
const ship = PurchaseOrder.ShipmentDate;
$('#ShipmentDate').val(ship ? $.formatDate(ship) : "");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.