首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查value是否为"null“

检查value是否为"null“
EN

Stack Overflow用户
提问于 2020-09-23 01:21:19
回答 1查看 67关注 0票数 0

我正在尝试检查一个值是否为"null“,但我不知道如何检查。下面是我的代码:

代码语言:javascript
复制
 Milestone: any={};
constructor(public toastController: ToastController) {
    this.Milestone.MilestoneType='';
    this.Milestone.date='';
    this.Milestone.Comment='';
  }
onSubmit(){
  var Milestone = JSON.stringify({
    MilestoneType: this.Milestone.MilestoneType, 
    date: this.Milestone.date,
    Comment: this.Milestone.Comment,
    ID: this.current.id});
    
   if( this.Milestone.date=="null"){
     this.DateToast();
   }
   else if( this.Milestone.Commment==='null'){
    this.CommentToast();
  } 
  else{
...
}

MilestoneTypedateComment的值来自HTML文件:

代码语言:javascript
复制
<ion-segment name="MilestoneType" [(ngModel)]="Milestone.MilestoneType" [ngModelOptions]="{standalone: true}" >
        <ion-segment-button value="Plant" >Plant</ion-segment-button>
        <ion-segment-button value="Fertilizer" >Fertilizer</ion-segment-button>
        <ion-segment-button value="Other" >Other</ion-segment-button>
      </ion-segment>
    </ion-toolbar>
    <div [ngSwitch]="true" >
      <ion-card *ngSwitchCase="Milestone.MilestoneType=== 'Plant' || Milestone.MilestoneType==='Fertilizer' || Milestone.MilestoneType==='Other'">
          <p>Type: {{Milestone.MilestoneType}}</p>
          <label for="date">Date: </label>
          <input required name="date" [(ngModel)]="Milestone.date"  type="date" clearInput="true">
          <br>
        <div class="form-group">
          <label for="Comment" >Comment:</label>
          <input id="comment" name="Comment" [(ngModel)]="Milestone.Comment" clearInput="true">
      </div>

我的目标是检查一个值是否为null .If,因此,我需要显示一条消息。我在If结构中尝试了以下内容:this.Milestone.date=="null" this.Milestone.date==="null" Milestone.date=="null" Milestone.date==="null"

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-23 01:52:31

当你把null和"null“放在一起时,它就是一个字符串。如果您只是想检查字段是否为空,请尝试以下操作

代码语言:javascript
复制
if(!this.Milestone.date){ //empty date field
   this.DateToast();
}
if(!this.Milestone.Commment){//empty comment field
   this.CommentToast();
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64014734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档