javascript - How to print empty when data = null - Stack Overflow

How to print" " when i get data = null.I tried this but it didn't work.var unit = $(th

How to print " " when i get data = null.

I tried this but it didn't work.

var unit = $(this).find(":selected").data("product_unit");
if(unit === "null") {
  unit = "";
}

How to print " " when i get data = null.

I tried this but it didn't work.

var unit = $(this).find(":selected").data("product_unit");
if(unit === "null") {
  unit = "";
}
Share Improve this question edited Oct 23, 2017 at 7:45 PreetyK 4615 silver badges15 bronze badges asked Oct 23, 2017 at 7:22 test1321test1321 3311 gold badge9 silver badges23 bronze badges 4
  • You want to show " " or just a space ` ` ? – Carsten Løvbo Andersen Commented Oct 23, 2017 at 7:23
  • 1 "null" is a string, it is not null ... – Teemu Commented Oct 23, 2017 at 7:23
  • console unit wheter unit is null or undefined if its null just check null – Akshay Commented Oct 23, 2017 at 7:23
  • should be null instead of "null" – test1321 Commented Oct 23, 2017 at 7:24
Add a ment  | 

7 Answers 7

Reset to default 2

change "null" to null. You are paring to a string.

Also add a space to unit = ""; to add a space if you want that.

var unit = $(this).find(":selected").data("product_unit");
if(unit === null) {
  unit = " ";
}

Try null with out quotation marks

var unit = $(this).find(":selected").data("product_unit");
if(unit === null) {
  unit = "";
}

try with this one

if (unit.trim() == '') {
  unit = "";
}

var unit = $(this).find(":selected").data("product_unit").val();
if(unit.empty()) {
  unit = "";
}

short form:

var unit = $(this).find(":selected").data("product_unit") || "";

if product_unit is a boolean false (false, null, empty string etc) gets overwritten to empty string

Try it:

var unit = $(this).find(":selected").data("product_unit") || "";

You can use .length for printing empty.

The length property returns the length of a string (number of characters).

The length of an empty string is 0.

var unit = $(this).find(":selected").data("product_unit");
if(unit.length === 0) {
  unit = " ";
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208722a4616716.html

相关推荐

  • javascript - How to print empty when data = null - Stack Overflow

    How to print" " when i get data = null.I tried this but it didn't work.var unit = $(th

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信