layui获取select的option后设置选中、只读不可操作

今天找了一天最后结合某个网友的方法重新写了个方法

//contype 为查询出来select的值
if(contype){
    //设置动态获取option后选中
	$('select[name="contype"]').next().find('.layui-anim')
     			.children('dd[lay-value="'+contype+'"]').click();
	//根据状态判断只读
	if(status=='search'){
		//设置disabled属性也可以实现只读,但是字体是灰白色  看不清 
	    //$('#contype').attr('disabled',"disabled");
		//获取到layui渲染select时的input直接设置值
		$('.layui-select-title').find("input").val(contype);
		//删除掉原来的select
		$('#contype').remove();

            //第二天发现只remove原生select有一定几率失败,所以新加删除 dl
            $('dl').remove();
      }
}

后续业务又有相关【select只读】需求对上面代码进行了优化

if(assetstype){
    var opttext=$('select[name="assetstype"]').find("option:selected").text();
    $('select[name="assetstype"]').next().find("input").val(opttext);
    $('#assetstype').remove();
	$('dl').remove();
}

 

 

 

 

 

 

你可能感兴趣的:(layui)