【jquery】layer插件select选中默认值

前言

再次更改代码的时候,因为城市下拉列表是从数据库查出来的,所以这时候就想到了一起以前用到的一个方法:select重新渲染
就是把未渲染的元素该怎么设置值还怎么设置值,然后把layer渲染出来的页面样式,从新再渲染一次, ,,
示例: layui渲染文档

// 一大堆需要设置的值,然后一个渲染,就可以
$("#userName).val("小明");
$("#city").val("天剑山");
// 更新全部
form.render(); 
// 刷新select选择框渲染
form.render('select');

丢弃

--------------------------------------------------------------------
--------------------------------------------------------------------
	
/**
 * layui:select插件,默认选中
 * ps:单个下拉框
 * @param 下拉框的id
 * @param 想要让选中的值:str
 */
function layuiSelected(id,str){
    //0、设置select的值
    $("#"+id).attr("value",str);
    //0.1把select下的option的selected换成现在的
    $("#"+id).children("option").each(function(){
    	if ($(this).text() == str) {
    		$(this).attr("selected","selected");
		}else{
			if ($(this).attr("selected") == "selected") {
				$(this).removeAttr("selected");
			}
		}
    });
	//1、首先设置输框
	$("#"+id).siblings("div[class='layui-unselect layui-form-select']").children("div[class='layui-select-title']").children("input").val(str);
	//2、其次,设置dl下的dd
	$("#"+id).siblings("div[class='layui-unselect layui-form-select']").children("dl").children("dd").each(function(){
		if ($(this).text() == str){
			if (!$(this).hasClass("layui-this")) {
				$(this).addClass("layui-this");
				$(this).click();
			}
			return true;
		}else{
			if ($(this).hasClass("layui-this")) {
				$(this).removeClass("layui-this");
			}
		}
	});
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值