SoFunction
Updated on 2025-03-10

The method of selecting the default value in layer plugin

When changing the code again, because the city drop-down list was found from the database, I thought of a method I used before: select re-render

It is to set the value of unrendered elements and how to set the value, and then re-render the page style rendered by the layer, ,

Example:[layui rendering document](/doc/modules/#render)

  $("#userName).val("Xiao Ming");  ...
  $("#city").val("Tianjian Mountain");  ...A lot of values ​​that need to be set,Then a render,That's it
  (); //Update all  ('select'); //refreshselectSelect box rendering

The abandoned below! ! ! ! ! ! ! ! ! ! ! ! ! !

/**
   * layui: select plugin, selected by default
   * ps: single drop-down box
   * @param drop-down box id
   * @param Want to make the selected value: str
   */
  function layuiSelected(id,str){
    //0. Set the value of select    $("#"+id).attr("value",str);
    //0.1 Change the selected option under select to the current one    $("#"+id).children("option").each(function(){
      if ($(this).text() == str) {
        $(this).attr("selected","selected");
      }else{
        if ($(this).attr("selected") == "selected") {
          $(this).removeAttr("selected");
        }
      }
    });
    //1. Set the input frame first    $("#"+id).siblings("div[class='layui-unselect layui-form-select']").children("div[class='layui-select-title']").children("input").val(str);
    //2. Secondly, set dd under dl    $("#"+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");
        }
      }
    });
  }

The above method of selecting the default value of the layer plug-in select is all the content I share with you. I hope you can give you a reference and I hope you can support me more.