我有了這個jQuery腳本如何阻止NaN在結果中顯示?
$(document).ready(function() {
$(function(){
$("#result").submit(function(e) {
e.preventDefault();
var ele = $("#element").val(),
target = $("#target").val(),
context = $("#context").val(),
border = $("#border").val(),
margin = $("#margin").val(),
padding = $("#padding").val();
console.log(ele, target, context, border, margin, padding);
var DoubleMargin = parseInt(margin, 10) * 2;
var DoublePadding = parseInt(padding, 10) * 2;
var DoubleBorder = parseInt(border, 10) * 2;
var ActualTarget = parseInt(target, 10) - parseInt(DoubleBorder, 10) - parseInt(DoubleMargin, 10) - parseInt(DoublePadding, 10) * 1;
var result3 = parseInt(target, 10) - parseInt(DoubleMargin, 10) * 1;
var MarginResult = parseInt(margin, 10)/parseInt(target, 10) * 100;
var PaddingResult = parseInt(padding, 10)/parseInt(target, 10) * 100;
var OriginalResult = parseInt(ActualTarget, 10)/parseInt(context, 10) * 100;
var BorderResult = parseInt(target, 10) - parseInt(border, 10) * 1;
//$(".result").append(ele + " " + result + "%");
$("<p></p>", {
html: ele + " {<br><span>width: " + OriginalResult + "%;" + " /* " + ActualTarget + " (originally " + target + ")/" + context + " */ " + "<br>border: " + border + "px; " + "<br>margin: " + MarginResult + "%; " + "<br>padding: " + PaddingResult+ "%;" + "<br> </span>}"
}).hide().appendTo("#code-results").fadeIn();
});
});
});
但當有人完全不輸入數字我的成績得到喃?我該如何擺脫這個代碼?我也想0仍然顯示爲0這是可能的呢?
編輯:其實我當有人不輸入數字,而不是NaN的,我得到0的結果的愛,這可能嗎?測試多個值的實現:
如果你總是想要的結果,你也可以用'>>> 0'作爲數字變量的前綴,將無效數字設置爲0.副作用:數字範圍將爲'[0,4294967295]'。 – 2012-03-17 09:47:50