layer插件select選中默認值的方法
更新時間:2018年08月14日 14:26:06 作者:樹林中的啄木鳥
今天小編就為大家分享一篇layer插件select選中默認值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
再次更改代碼的時候,因為城市下拉列表是從數(shù)據(jù)庫查出來的,所以這時候就想到了一起以前用到的一個方法:select重新渲染
就是把未渲染的元素該怎么設(shè)置值還怎么設(shè)置值,然后把layer渲染出來的頁面樣式,從新再渲染一次, ,,
示例: [layui渲染文檔](http://www.layui.com/doc/modules/form.html#render)
$("#userName).val("小明");
...
$("#city").val("天劍山");
...一大堆需要設(shè)置的值,然后一個渲染,就可以了
form.render(); //更新全部
form.render('select'); //刷新select選擇框渲染
下面的廢棄!?。。。。。。。。。。。?!
/**
* layui:select插件,默認選中
* ps:單個下拉框
* @param 下拉框的id
* @param 想要讓選中的值:str
*/
function layuiSelected(id,str){
//0、設(shè)置select的值
$("#"+id).attr("value",str);
//0.1把select下的option的selected換成現(xiàn)在的
$("#"+id).children("option").each(function(){
if ($(this).text() == str) {
$(this).attr("selected","selected");
}else{
if ($(this).attr("selected") == "selected") {
$(this).removeAttr("selected");
}
}
});
//1、首先設(shè)置輸框
$("#"+id).siblings("div[class='layui-unselect layui-form-select']").children("div[class='layui-select-title']").children("input").val(str);
//2、其次,設(shè)置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選中默認值的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于JavaScript實現(xiàn)實時在線協(xié)作編輯器
隨著Web技術(shù)的發(fā)展,實現(xiàn)在線協(xié)作編輯文檔已經(jīng)成為一種常見的需求,本文主要為大家詳細介紹了如何使用JavaScript實現(xiàn)實時在線協(xié)作編輯器,需要的可以參考下2024-01-01

