如何检查值是否为数字。如果是数值,则显示文本中心,否则保留文本。我想知道我的陈述有什么问题。
for (var l = 0; l < Object.keys(pods[i].data.summaryDetailedData).length; l++) {
contentHtml += "<tr>";
for (var m = 0; m < pods[i].data.columns.length; m++) {
for (var field in pods[i].data.summaryDetailedData[l]) {
var rowspan = (pods[i].data.summaryDetailedData[l].children !== undefined ? pods[i].data.summaryDetailedData[l].children.length : "");
rowspanMax = Math.max(rowspanMax, rowspan);
if (field === pods[i].data.columns[m]) {
var preFix = pods[i].data.summaryDetailedData[l]["sPrefix"] !== undefined ? pods[i].data.summaryDetailedData[l]["sPrefix"] : "";
var postFix = pods[i].data.summaryDetailedData[l]["sPostfix"] !== undefined ? pods[i].data.summaryDetailedData[l]["sPostfix"] : "";
var value = pods[i].data.summaryDetailedData[l][field];
if (value.toString().substr(0, 3) == "- 1") {
value = "N/A";
}
var color = pods[i].data.summaryDetailedData[l][field + "_color"];
if (colName[m] === "sLabel" && pods[i].data.summaryDetailedData[l].bStepThrough == true) {
value = "<a href=\"#\" class=\"stepthrough\">" + value + "</a>";
}
color = color !== "" && color !== undefined ? " <span class=\"color\" style=\"background: #" + color + "\"></span>" : " <span class=\"color\"></span>";
contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
if (rowspan > 1) {
var rowspanContent = "<td rowspa1=\"" + rowspan + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
}
if (field === "sLabel") {
for (var child in pods[i].data.summaryDetailedData[l].children) {
if (child > 0 && rowspan >= 2) {
contentHtml += "</tr>";
contentHtml += "<tr>";
contentHtml += rowspanContent;
}
&#13;
答案 0 :(得分:1)
您使用typeof
:
var ff=1.222; console.log(typeof ff) // number
(typeof ff == "number") // true
var ff="1.222"; console.log(typeof ff) // string
(typeof ff == "number") // false
或强>
使用toFixed
方法,如果变量不是数字,则返回undefined
,否则,数字对象将在其原型中定义函数,因此它将返回函数{{因此它本身就是一个数字。
(这有点像黑客,但它效率高,成本最低)
toFixed
答案 1 :(得分:0)
使用Number.isFinite(value):
contentHtml += "<td class=\"" + (Number.isFinite(value) ? "text-center" : "text-left" + "\">" ) + value + (Number.isFinite(value) ? preFix : "") + color + (Number.isFinite(value) ? postFix : "") + "</td>";
答案 2 :(得分:0)
这是jQuery 3.0当前使用的检查:
function isNumeric(obj){
// As of jQuery 3.0, isNumeric is limited to
// strings and numbers (primitives or objects)
// that can be coerced to finite numbers (gh-2662)
var type = jQuery.type(obj);
return (type === "number" || type === "string") &&
// parseFloat NaNs numeric-cast false positives ("")
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
// subtraction forces infinities to NaN
!isNaN(obj - parseFloat(obj));
}
所以如果你还是使用jQuery,为什么不使用内置的isNumeric()函数呢?