Jquery 从下拉列表中检索所选内容并填充列表

Jquery 从下拉列表中检索所选内容并填充列表,jquery,element,populate,Jquery,Element,Populate,我需要检索页面上每个下拉列表的所有选定值,并用这些值填充无序列表。当然,每个项目都有一个新的li元素 目前我有: $(document).ready(function() { $("select").each(function() { var sel = ""; $(this).find(":selected").each(function() { sel += $(this).text() + " "; });

我需要检索页面上每个下拉列表的所有选定值,并用这些值填充无序列表。当然,每个项目都有一个新的li元素

目前我有:

$(document).ready(function() {
    $("select").each(function() {
        var sel = "";
        $(this).find(":selected").each(function() {
            sel += $(this).text() + " ";
        });
        $(".size-result").text(sel);
    });
});​
如何使用它创建新的li元素,然后插入文本

多亏了威利,我才有了这样的工作:

$(document).ready(function() {
    $("select").change(function() {
        var sel = "";
    $(".option-select option:selected").each(function () {
       sel += $(this).text() + " ";
    });
    $(".result").empty().append(sel);
    }); 
});
有什么方法可以否定每个下拉列表的第一个条目吗?因为这是一个选择。。。或者选择一个。。。值我想阻止它显示,因为它是select中的第一个值

$("select option[selected]").each(function(i,e) {
            $("ul#list").append("<li>"+$(e).val()+"</li>");})​;
编辑:

您可以侦听每个下拉列表中的更改,并重新更新每个更改的列表

$(document).ready(function() {        
    $('select').change(function(){ // <-- bind change event handler to the drop downs
        var newLI = '';
        $("select option:selected").each(function() {   // <-- then iterate each time it changes   
              newLI += '<li>' + $(this).text() + "</li>";              
        });
        $('ulselector').empty().append(newLI); //<-- empty() to remove old list and update with new list
    });
});​
再次编辑:

您可以检查所选值的索引是否为0。。如果是,那么就不要显示

$(document).ready(function() {
    $('select').change(function() { // <-- bind change event handler to the drop downs
        var newLI = '';

        $("select option:selected").each(function() { // <-- then iterate each time it changes   
            if ($(this).index() !== 0) { // <-- only if not default
                newLI += '<li>' + $(this).text() + "</li>";
            }    
            $('ul').empty().append(newLI); //<-- empty() to remove old list and update with new list
        });
    });
});

谢谢现在我必须弄清楚当下拉列表中的选择发生变化时如何更新li列表。有什么提示吗?在改变了这两个不一致性之后,代码似乎不起作用了。当然我没怎么搞砸。非常感谢你的帮助。我已经编辑了我的问题,谢谢你的帮助!有没有办法否定所有选择中的第一个选项,因为它们和选择一一样都是占位符。。等等。一种防止它们显示在填充列表中的方法。@AjTroxell我想这就是你想要的。。除非你想要空白的李作为一个空间持有人-我更新了答案惊人!非常感谢你!
$(document).ready(function() {
    $('select').change(function() { // <-- bind change event handler to the drop downs
        var newLI = '';

        $("select option:selected").each(function() { // <-- then iterate each time it changes   
            if ($(this).index() !== 0) { // <-- only if not default
                newLI += '<li>' + $(this).text() + "</li>";
            }    
            $('ul').empty().append(newLI); //<-- empty() to remove old list and update with new list
        });
    });
});
var selectedArr = [];
var $ul = $('<ul />');
selectedArr = $("select option[selected=selected]").map(function() {
    return this.value
}).get();
$.each(selectedArr, function() {
   $ul.append('<li>' + this.slice(3));
});
$('body').append($ul);
$("select option[selected]").each( function() {
        $('<li>', {
             text: $(this).val()
        }).appendTo('ul#list');
});