请教一个闭包的问题?

function setUp(data){

    var s = ["s1", "s2", "s3"],

        data = ['test1','test2','test3'];

    if (s) {

        s.forEach(function (i) {

            $("#" + i).find('option').first().val('');

            if (data) {

                data.forEach(function (item) {

                

                  //三级联动的select的动态渲染在DOM里的,

                  //现在需要将data里的值分别放进对应下标的select的第一个option里.

                  

                });

            }

        })

    }

}

<select id='s1'></select>

<select id='s2'></select>

<select id='s3'></select>


江户川乱折腾
浏览 517回答 1
1回答

狐的传说

function setUp(data){&nbsp; &nbsp; var s = ["s1", "s2", "s3"],&nbsp; &nbsp; &nbsp; &nbsp; data = ['test1','test2','test3'];&nbsp; &nbsp; if (s) {&nbsp; &nbsp; &nbsp; &nbsp; s.forEach(function (i,index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _option = $("#" + i).find('option');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(_option.length == 0){//初始化。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#" + i).html("<option>"+(data && data[index])+"</option>");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{//取第一个&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _option.eq(0).text((data && data[index]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}这样呢?初始化的时候,节点选错了,已修改
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript