admin管理员组

文章数量:1415684

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 = " ";
}

本文标签: javascriptHow to print empty when datanullStack Overflow