parseInt, parseFloat, Number... i dont know
Hi Does someone know why this just wont work!! i have tried alsorts.
function loadJcrop(widthN, heightN){
var run = true;
if( run === true ) {
alert( parseInt(Number(widthN) / Number(heightN)) );
jQuer开发者_开发百科y(function(widthN, heightN) {
jQuery('#cropbox').Jcrop({
onChange: showCoords, onSelect: showCoords, aspectRatio: parseInt(Number(widthN) / Number(heightN))
});
});
}
}
jQuery(window).load(function() {
loadJcrop(16, 1);
});
the alert returns 16 but nothing!!
and if i put
aspectRatio: 16
it works
any ideas??
First, I would make the calculations in one spot. Second, I think you're better off Math.round. Most importantly, however, you are using function parameters in your inner function, and I don't think you want to do that (it will unset the variables heightN and widthN).
I would re-write that function as:
function loadJcrop(widthN, heightN){
// This is much clearer and easier to understand.
var current = Math.round( Number(widthN) / Number(heightN) );
var run = true;
if( run ) { // if you have a variable which is a Boolean,
// you should not use '=='
alert( current );
jQuery(function(/* Note the empty parameters */) {
jQuery('#cropbox').Jcrop({
onChange: showCoords, onSelect: showCoords, aspectRatio: current)
});
});
}
}
精彩评论