Super simple JS two level multi level linkage simple example

  • 2020-03-30 01:44:40
  • OfStack

Super simple JS linkage code, but to cooperate with jquery to use, you can also modify their own not optional JS library code

<tr>
  <th> The primary classification </th><td>
    <select name="subsidiary_cat_id" class="subsidiary_cat_id" onchange="linkage('.subsidiary_cat_id','.cat_id');">
      <option> Please select a </option>
    </select>
    <script language="javascript">
    var linkage_data={$list_file_category};//Initialize the cascading data, {$list_file_category} is the data of PHP's JSON_ENCODE, the array has 3 data, id,parent_id,name
    function linkage(parent_dom,son_dom){
        var parent_id=0;
        if(parent_dom){
            parent_id=$(parent_dom).val();    
        }
        $(son_dom).empty();//So let's clear the drop down
        var html='<option> Please select a </option>';
        $.each(linkage_data,function(key,value){
            if(value.parent_id==parent_id){
                html+="<option value='"+value.id+"'>"+value.name+"</option>";
            }
        });
        $(son_dom).append(html);
    }
    linkage('',".subsidiary_cat_id")//Do not pass the parent node, represented as the first level of data
    </script>
    </td></tr>
<tr><th> The secondary directory </th><td>
  <select name="cat_id" class="cat_id">
    <option> Please select a </option>
  </select></td></tr>

Related articles: