123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- function randomString() {
- len = 8;
- let timestamp = new Date().getTime();
- /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
- let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
- let maxPos = $chars.length;
- let randomStr = '';
- for (let i = 0; i < len; i++) {
- randomStr += $chars.charAt(Math.floor(Math.random() * maxPos));
- }
- return timestamp + randomStr;
- }
-
- function go(url){
- window.location = url;
- }
- /**
- * 格式化数字
- * @param num 数字
- * @param ext 保留多少位小数
- * @returns {*}
- */
- function number_format(num, ext){
- if(ext < 0){
- return num;
- }
- num = Number(num);
- if(isNaN(num)){
- num = 0;
- }
- var _str = num.toString();
- var _arr = _str.split('.');
- var _int = _arr[0];
- var _flt = _arr[1];
- if(_str.indexOf('.') == -1){
- /* 找不到小数点,则添加 */
- if(ext == 0){
- return _str;
- }
- var _tmp = '';
- for(var i = 0; i < ext; i++){
- _tmp += '0';
- }
- _str = _str + '.' + _tmp;
- }else{
- if(_flt.length == ext){
- return _str;
- }
- /* 找得到小数点,则截取 */
- if(_flt.length > ext){
- _str = _str.substr(0, _str.length - (_flt.length - ext));
- if(ext == 0){
- _str = _int;
- }
- }else{
- for(var i = 0; i < ext - _flt.length; i++){
- _str += '0';
- }
- }
- }
-
- return _str;
- }
- /**
- * 设置用户输入数字合法性
- * @param name 表单name
- * @param min 范围最小值
- * @param max 范围最大值
- * @param keep 保留多少位小数 可不填
- * @param def 不在范围返回的默认值 可不填
- */
- function checkInputNum(name,min,max,keep,def){
- var input = $('input[name='+name+']');
- var inputVal = parseInt(input.val());
- var a = parseInt(arguments[3]) ? parseInt(arguments[3]) : 0;//设置第四个参数的默认值
- var b = parseInt(arguments[4]) ? parseInt(arguments[4]) : '';//设置第四个参数的默认值
- if(isNaN(inputVal)){
- input.val('');
- }else{
- if(inputVal < min || inputVal > max){
- if(a > 0){
- input.val(number_format(b,a));
- }else{
- input.val(b);
- }
- }else{
- if(a > 0){
- input.val(number_format(inputVal, a));
- }else{
- input.val(inputVal);
- }
- }
- }
- }
-
- //图片垂直水平缩放裁切显示
- (function($){
- $.fn.VMiddleImg = function(options) {
- var defaults={
- "width":null,
- "height":null
- };
- var opts = $.extend({},defaults,options);
- return $(this).each(function() {
- var $this = $(this);
- var objHeight = $this.height(); //图片高度
- var objWidth = $this.width(); //图片宽度
- var parentHeight = opts.height||$this.parent().height(); //图片父容器高度
- var parentWidth = opts.width||$this.parent().width(); //图片父容器宽度
- var ratio = objHeight / objWidth;
- if (objHeight > parentHeight && objWidth > parentWidth) {
- if (objHeight > objWidth) { //赋值宽高
- $this.width(parentWidth);
- $this.height(parentWidth * ratio);
- } else {
- $this.height(parentHeight);
- $this.width(parentHeight / ratio);
- }
- objHeight = $this.height(); //重新获取宽高
- objWidth = $this.width();
- if (objHeight > objWidth) {
- $this.css("top", (parentHeight - objHeight) / 2);
- //定义top属性
- } else {
- //定义left属性
- $this.css("left", (parentWidth - objWidth) / 2);
- }
- }
- else {
- if (objWidth > parentWidth) {
- $this.css("left", (parentWidth - objWidth) / 2);
- }
- $this.css("top", (parentHeight - objHeight) / 2);
- }
- });
- };
- })(jQuery);
-
- function trim(str) {
- return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
- }
-
- /* 显示Ajax表单 */
- function ajax_form(id, title, url, width, model)
- {
- if (!width) width = 480;
- if (!model) model = 1;
- var d = DialogManager.create(id);
- d.setTitle(title);
- d.setContents('ajax', url);
- d.setWidth(width);
- d.show('center',model);
- return d;
- }
- //显示一个内容为自定义HTML内容的消息
- function html_form(id, title, _html, width, model) {
- if (!width) width = 480;
- if (!model) model = 0;
- var d = DialogManager.create(id);
- d.setTitle(title);
- d.setContents(_html);
- d.setWidth(width);
- d.show('center',model);
- return d;
- }
-
- /*
- * 为低版本IE添加placeholder效果
- *
- * 使用范例:
- * [html]
- * <input id="captcha" name="captcha" type="text" placeholder="验证码" value="" >
- * [javascrpt]
- * $("#captcha").nc_placeholder();
- *
- * 生效后提交表单时,placeholder的内容会被提交到服务器,提交前需要把值清空
- * 范例:
- * $('[data-placeholder="placeholder"]').val("");
- * $("#form").submit();
- *
- */
- (function($) {
- $.fn.nc_placeholder = function() {
- var isPlaceholder = 'placeholder' in document.createElement('input');
- return this.each(function() {
- if(!isPlaceholder) {
- $el = $(this);
- $el.focus(function() {
- if($el.attr("placeholder") === $el.val()) {
- $el.val("");
- $el.attr("data-placeholder", "");
- }
- }).blur(function() {
- if($el.val() === "") {
- $el.val($el.attr("placeholder"));
- $el.attr("data-placeholder", "placeholder");
- }
- }).blur();
- }
- });
- };
- })(jQuery);
-
- function setCookie(name,value,days){
- var exp=new Date();
- exp.setTime(exp.getTime() + days*24*60*60*1000);
- var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
- document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString() +"path=/";
- }
- function getCookie(name){
- var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
- if(arr != null){
- return unescape(arr[2]);
- }
- return null;
- }
- function delCookie(name){
- var exp=new Date();
- exp.setTime(exp.getTime()-1);
- var cval=getCookie(name);
- if(cval!=null){
- document.cookie=name+"="+cval+";expires="+exp.toGMTString() +"path=/";
- }
- }
|