需要0与null不同

时间:2017-09-28 17:15:10

标签: javascript operators jsx

这是我在div标签中的代码:

<div className="lk-btn-group btn-group clear">
            {this.state.invoiceId ? <button className="btn btn-primary btn-sm pull-left" onClick={() => this.printInvoice()}>{abp.localization.localize('PrintInvoice')}</button> : null}
            {this.state.delivery.delivery && !this.state.invoiceId ? <button className="btn btn-primary btn-sm pull-left" onClick={() => this.printDeliveryNote()}>{abp.localization.localize('PrintDeliveryNote')}</button> : null}
            {this.state.delivery.delivery == null ? <button className="btn btn-primary btn-sm pull-right" onClick={() => {
             this.markAsDelivered()
            }} disabled={!this.state.beginningPercentage || !this.state.gallonsDelivered}>{this.props.completeDelivery}</button> : <span className="label label-default pull-right"><i className="fa fa-check"></i> {this.props.deliveryComplete}</span>} 
</div>

我的问题是如何输入0(零)到而不是与null相同?我需要能够让用户继续使用0并使我的禁用按钮显示而不是禁用。我相信它发生在上面div代码第四行的== null,在我看来是禁用&#34; btn btn-primary btn-sm pull right&#34;

如果用户还没有输入任何内容,这很好,但是我还需要将0视为输入的任何其他值,如果有意义的话,不仅仅为null或为空。我仍然需要所有其他功能保持不变。

1 个答案:

答案 0 :(得分:0)

我发现了这一点,希望对您有所帮助,基本上您可以使用typeof来检查this.state.delivery.delivery的类型

// typeof this.state.delivery.delivery is a number
if (typeof this.state.delivery.delivery === "number")

这个答案扩展了这个原则。 https://stackoverflow.com/a/4514622/6558939

您也可以尝试Number.isInteger(typeof this.state.delivery.delivery),它应该返回true或false,以满足三元运算符。

相关问题