search

Home  >  Q&A  >  body text

javascript - Ask a question about closure?

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>
为情所困为情所困2881 days ago249

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-05-18 10:52:55

    function setUp(data){
        var s = ["s1", "s2", "s3"],
            data = ['test1','test2','test3'];
        if (s) {
            s.forEach(function (i,index) {
                var _option = $("#" + i).find('option');
                if(_option.length == 0){//初始化。
                    $("#" + i).html("<option>"+(data && data[index])+"</option>");
                }else{//取第一个
                    _option.eq(0).text((data && data[index]));
                };
            })
        }
    }
    
    这样呢?初始化的时候,节点选错了,已修改

    reply
    0
  • Cancelreply