Control functions that can be cascaded by any level selector

Source: Internet
Author: User

Requirements

Provide data 1:

[    {        text: ' First level '        value: ' 1 ',        list:[            {                text: ' Second level '                value: ' 1.1 ',                list:[                    {                        Text: ' Tertiary '                        value: ' 1.1.1 ', list:[]}, ...]}    ,    ...]
Data 2:

<pre name= "Code" class= "JavaScript" > Cascade selector elements list <span style= "font-family:arial, Helvetica, Sans-serif;" >selectlist, such as [Select1,select2,select3,...] </span>


"Critical Code"

function Cascade (Selectlist,data) {//Here SelectList a list of selector elements that cascade, such as [Select1,select2,select3,...]    TODO for (Var i=0;i<selectlist.length;i++) {var temp_data=data;    for (Var j=0;j<i;j++) {temp_data=temp_data[0].list;    } fillselect (Selectlist[i],temp_data);    Add Change Event Selectlist[i].addeventlistener ("Changes", function (event) {var value=event.target.value;    var v_p=value.split (".");    var v_length=v_p.length;      If it is the last select, jump out if (v_length>=selectlist.length) return;    Constructs a new selector var newselectlist=[];    for (Var j=v_length;j<selectlist.length;j++) Newselectlist.push (Selectlist[j]);    alert (newselectlist.length);    Constructs a new data var newdata=data;    for (Var j=0;j<v_p.length;j++) {newdata=newdata[parseint (v_p[j]) -1].list;    } console.log (Newselectlist);    Console.log (NewData);    Cascade (Newselectlist,newdata);    }    ); }}//Update selectfunction fillselect (select,list) {select.innerhtml= ""; List.foreach (function (data) {VAR option=new option (Data.text,data.value); select.add (option);}); 

"Code Implementation"

<! DOCTYPE html>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Control functions that can be cascaded by any level selector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.