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 notnull
... – 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
7 Answers
Reset to default 2change "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
版权声明:本文标题:javascript - How to print empty when data = null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745208721a2647762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论