如何制作所选元素并附加选项?

时间:2012-08-11 11:17:23

标签: jquery

我正在尝试使用一些选项创建一个选择器组件。我没有得到任何结果。我这样做的方式出了点问题。

任何人都可以帮我吗?

var object = {
               "id":"countries",
               "label":"Country",
               "name":"countries",
               "type":"select",
               "options":[
                  {
                     "value":"",
                     "text":"Select Country"
                  },
                  {
                     "value":"in",
                     "text":"India",
                      "selected":"true"
                  },
                  {
                     "value":"us",
                     "text":"United Stated"

                  },
                  {
                     "value":"uk",
                     "text":"United Kingdom"
                  },
                  {
                     "value":"cn",
                     "text":"Canada"
                  }
               ]
};
var select = "";

var mapInfo = function (element,info) {
    $.map(info, function(val,num){
console.log(val);        
    })
}

var generateSelector = function (info){

    select +='<select'+info.id+'></select>'
    select +=mapInfo(select,info.options)

   $('body').append(select);

}(object);

jsfiddle here

5 个答案:

答案 0 :(得分:1)

试试这个:

var $select = $("<select id='"+obj.id+"' name='"+obj.name+"'/>");
var selected = '';
var generateSelector = function (info){
    $.each(obj.options, function(i, ob){
        if (ob.hasOwnProperty('selected')) {
             selected = ob.value;
        }
        $select.append('<option value='+ob.value+'>'+ ob.text+'</option>')
    })

   $('body').append($select);
   $('select').val(selected)
}

Demo

答案 1 :(得分:0)

使用JavaScript编写HTML可能会变得混乱。在这种情况下,您将id与select标记连接在一起。改为使用DOM:

var select = document.createElement("select"), i, opt; //create a select element
select.id = info.id;  //set the id
for(i = 0; i<info.options.length; i++) {
    opt = new Option();  //create a new option and set its properties
    opt.text = info.options[i].text;
    opt.value = info.options[i].value;
    opt.selected = info.options[i].selected;
    select.appendChild(opt); //add the option to the select element
}

jsFiddle

答案 2 :(得分:0)

试试这个

function generateSelector(obj) {
var selector = $('<div></div>');
var label = $('<label for="' + obj.id + '">' + obj.label + '</label>');
var selectElem = $('<select id="' + obj.id + '" name="' + obj.name + '"></select>');
for (var i = 0; i < obj.options.length; i++) {
    var option = $('<option value="' + obj.options[i].value + '">' + obj.options[i].text + '</option>');
    if (obj.options[i].selected)
        option.attr('selected', 'selected');
    option.appendTo(selectElem);
}
label.appendTo(selector);
selectElem.appendTo(selector);

return selector;

}

答案 3 :(得分:0)

这是我的发电机版本。 :)

$(object).each(function(i,v) {
    var label = $("<label />", { html: v.label, 'for': v.name });
    var element;
    switch(v.type) {
        case "select":
            element = $("<select />", { id: v.id, name: v.name  });
            $(v.options).each(function(x,y) {
                var option = $("<option />", { value : y.value, html : y.text });
                element.append(option)
            });
        break;
    }
    $("body").append(label, element);        
});

[See it in action]

  • 这可以处理多组数据
  • 作为可扩展性,它可以扩展到不仅仅包括选择类型。

答案 4 :(得分:0)

 $(document).ready(function () {
        var jsonObject = {
            "id": "countries",
            "label": "Country",
            "name": "countries",
            "type": "select",
            "options": [
                  {
                      "value": "",
                      "text": "Select Country"
                  },
                  {
                      "value": "in",
                      "text": "India",
                      "selected": "true"
                  },
                  {
                      "value": "us",
                      "text": "United Stated"

                  },
                  {
                      "value": "uk",
                      "text": "United Kingdom"
                  },
                  {
                      "value": "cn",
                      "text": "Canada"
                  }
               ]
        };

        var selectedvalue = '';

        $("body").append($("<label />", { html: jsonObject.label, 'for': jsonObject.name }),
        $("<select id='ddlSelect' />", { id: jsonObject.id, name: jsonObject.name }).html($.map(jsonObject.options, function (entity, i) {
            if (entity.selected == "true")
                selectedvalue = entity.value;
            return '<option value="' + entity.value + '">' + entity.text + '</option>';
            //return $("<option />", { value: entity.value, text: entity.text });
        }).join('')));

        $('#ddlSelect').val(selectedvalue).trigger('change');
    });

for demo请参阅此链接:http://jsfiddle.net/nanoquantumtech/vUwVq/