Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Created June 17, 2010 13:40
Show Gist options
  • Save sergio-fry/442136 to your computer and use it in GitHub Desktop.
Save sergio-fry/442136 to your computer and use it in GitHub Desktop.
jquery.extra.js
;(function($){
jQuery.fn.serializeJSON = function(){
var aData = $(this).serializeArray();
var result = {}
for(key in aData){
result[aData[key].name] = aData[key].value;
}
return result;
}
jQuery.fn.maxlengthWithCounter = function(){
return $(this).each(function(){
var counter = $("<span>").addClass("counter").setUniqId();
$(this).after(counter);
$(this).maxlength({feedback: "#" + counter.attr('id'), useInput: false });
});
};
jQuery.fn.setUniqId = function(){
return $(this).each(function(){
jQuery.setUniqId($(this));
});
};
jQuery.setUniqId= function(obj){
if($(this).attr("id") == "" || $("[id='"+$(this).attr("id")+"']").length != 1){
id = "rand_id_" + Math.random();
while($("[id='"+id+"']").length > 0){
id = "rand_id_" + Math.random();
}
$(this).attr("id", id);
}
}
jQuery.fn.intValue = function(){
var value;
if($(this).is(":input")){
value = $(this).val();
} else {
value = $(this).text();
}
value = parseInt(value);
if(isNaN(value)){
value = 0;
}
return value;
};
jQuery.fn.integerAutocorrection = function(){
return $(this).live("keyup", function(){
value = parseInt($(this).val());
if(isNaN(value)){
$(this).val('');
} else {
$(this).val(value);
}
});
};
jQuery.fn.floatAutocorrection = function(){
return $(this).live("keyup", function(){
value = parseFloat($(this).val());
if(isNaN(value)){
$(this).val('');
} else {
$(this).val(value);
}
});
};
$.fn.disableInputs = function(){
if($(this).is(":input")){
return $(this).attr('disabled', 'disabled');
} else {
return $(this).find(":input").attr('disabled', 'disabled');
}
};
$.fn.enableInputs = function(){
if($(this).is(":input")){
return $(this).attr('disabled', '');
} else {
return $(this).find(":input").attr('disabled', '');
}
};
})(jQuery);
/**
* Возвращает единицу измерения с правильным окончанием
*
* @param {Number} num Число
* @param {Object} cases Варианты слова {nom: 'час', gen: 'часа', plu: 'часов'}
* @return {String}
*/
function units(num, cases) {
num = Math.abs(num);
var word = '';
if (num.toString().indexOf('.') > -1) {
word = cases.gen;
} else {
word = (
num % 10 == 1 && num % 100 != 11
? cases.nom
: num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20)
? cases.gen
: cases.plu
);
}
return word;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment