首页 > 编程 > JavaScript > 正文

layer插件select选中默认值的方法

2019-11-19 13:15:39
字体:
来源:转载
供稿:网友

再次更改代码的时候,因为城市下拉列表是从数据库查出来的,所以这时候就想到了一起以前用到的一个方法:select重新渲染

就是把未渲染的元素该怎么设置值还怎么设置值,然后把layer渲染出来的页面样式,从新再渲染一次, ,,

示例: [layui渲染文档](http://www.layui.com/doc/modules/form.html#render)

  $("#userName).val("小明");  ...  $("#city").val("天剑山");  ...一大堆需要设置的值,然后一个渲染,就可以了  form.render(); //更新全部  form.render('select'); //刷新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");        }      }    });  }

以上这篇layer插件select选中默认值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表