1.判断字符是否为空的方法
function isEmpty ( obj ) {
if ( typeof obj == "undefined" || obj == null || obj == "" ) {
return true ;
} else {
return false ;
}
}
2.下拉回显,传值:下拉的id、要回显的值
function select ( sel, data ) {
$ ( "#" + sel) . children ( "option" ) . each ( function ( ) {
if ( this . value == data) {
$ ( this ) . attr ( "selected" , "selected" ) ;
}
} ) ;
}
3.指定日期天數后的日期
function addDays ( date, days ) {
var dd = new Date ( date) ;
dd. setDate ( dd. getDate ( ) + days) ;
var y = dd. getFullYear ( ) ;
var m = ( dd. getMonth ( ) + 1 ) < 10 ? "0" + ( dd. getMonth ( ) + 1 ) : ( dd. getMonth ( ) + 1 ) ;
var d = dd. getDate ( ) < 10 ? "0" + dd. getDate ( ) : dd. getDate ( ) ;
return y + '-' + m + '-' + d;
}
4.多选回显,参数:所有选中的值;多选的id;多选的值
function showChecked ( classVal, idVal, value ) {
if ( classVal. indexOf ( value) != - 1 ) {
$ ( "#" + idVal) . attr ( "checked" , "checked" ) ;
}
}
5.name类型的多选回显,参数:name值,所有选中的值
function showCheckBoxChecked ( name, val ) {
var rehabilitationProject = val;
var rehabilitationProjectArr = rehabilitationProject. split ( "," ) ;
$ ( "input[name='" + name+ "']" ) . each ( function ( ) {
for ( var i= 0 ; i< rehabilitationProjectArr. length; i++ ) {
if ( $ ( this ) . val ( ) == rehabilitationProjectArr[ i] ) {
$ ( this ) . prop ( "checked" , true ) ;
}
}
} ) ;
}
6.单选回显,参数:name值,数据库中的值
function showRadioChecked ( name, val ) {
$ ( "input:radio[name='" + name+ "']" ) . each ( function ( ) {
var str= $ ( this ) . val ( ) ;
if ( str== '' + val+ '' ) {
$ ( this ) . prop ( "checked" , true ) ;
}
} )
}
7.传入输入框的id和下拉框的id,使得下拉框选择完输入框同步绑定值(可选择可搜索可输入的下拉)
function editSelect ( val, select ) {
layui. use ( [ 'form' , 'layer' , 'tree' , 'laydate' , 'upload' ] , function ( ) {
var form = layui. form
layer = parent. layer === undefined ? layui. layer : top. layer,
laypage = layui. laypage,
upload = layui. upload,
layedit = layui. layedit,
laydate = layui. laydate,
$ = layui. jquery;
selectInput ( ) ;
function selectInput ( ) {
form. on ( 'select(' + select + ')' , function ( data ) {
var select_text = data. elem[ data. elem. selectedIndex] . text;
$ ( "#" + val + "" ) . val ( select_text) ;
$ ( "#" + select + "" ) . next ( ) . find ( "dl" ) . css ( { "display" : "none" } ) ;
form. render ( ) ;
} ) ;
$ ( '#' + val + '' ) . bind ( 'input propertychange' , function ( ) {
var value = $ ( "#" + val + "" ) . val ( ) ;
$ ( "#" + select + "" ) . val ( value) ;
form. render ( ) ;
$ ( "#" + select + "" ) . next ( ) . find ( "dl" ) . css ( { "display" : "block" } ) ;
var dl = $ ( "#" + select + "" ) . next ( ) . find ( "dl" ) . children ( ) ;
var j = - 1 ;
for ( var i = 0 ; i < dl. length; i++ ) {
if ( dl[ i] . innerHTML. indexOf ( value + "" ) <= - 1 ) {
dl[ i] . style. display = "none" ;
j++ ;
}
if ( j == dl. length - 1 ) {
$ ( "#" + select + "" ) . next ( ) . find ( "dl" ) . css ( { "display" : "none" } ) ;
}
}
} ) ;
}
} )
}
8.页面打开光标定位到某输入框
setTimeout ( function ( ) {
var onFocus = document. querySelector ( '.test' ) ;
onFocus. focus ( ) ;
} , 500 )
9.监听回车按钮
$ ( document) . keydown ( function ( event ) {
if ( event. keyCode== 13 ) {
$ ( "#test" ) . click ( ) ;
}
} ) ;