2009-07-13 54 views
4

我有一个下拉列表:jQuery的追加UL与李从按钮下拉列表值单击

<select id="ContentList" name="ContentList"> 
    <option value="">Please Select</option> 
    <option value="TEST_TOP">TEST TOP</option> 
</select> 

我有一个排序列表:

<ul id="sortable"> 
    <li class="ui-state-default">First</li> 
    <li class="ui-state-default">Second</li> 
    <li class="ui-state-default">Third</li> 
</ul> 

我有一个按钮:

<input type="button" value="Add Section" id="btnAdd" class="button"/> 

而且下面的脚本:

<script type="text/javascript"> 
     $(function() { 


      $("#sortable").sortable({ 
       placeholder: 'ui-state-highlight' 
      }); 
      $("#sortable").disableSelection(); 

      $('#btnAdd').click(function() { 

      }); 

     }); 

    </script> 

当用户选择下拉列表中的某些内容并单击添加时,我希望将其作为具有类属性等的<li>发送到可排序列表,现在下拉菜单显示“请选择”选项。

不知道最好的方法服用。由于

回答

6
$("#sortable").append("<li class='ui-state-default'>"+ 
          $("#ContentList option:selected").text()+"</li>"); 
$("#ContentList option:selected").remove(); 

应该做的伎俩...(:

+1

我 去,如果($( “#ContentList选项:选择”)VAL() != “”) { $( “#排序”。)附加( “” + $( “#ContentList选项:选择了”) VAL()+“。 “); $(”#ContentList option:selected“)。remove(); $('#ContentList')。attr('selectedIndex',0); } – Jon 2009-07-13 11:19:46

+0

不要在js中嵌入html,如果您更改标记,则需要两次更改!坏 – redsquare 2009-07-13 11:23:23

6

工作演示here

$('#btnAdd').click(function() { 

    //cache sortable 
    var $sortable = $("#sortable"); 

    //clone sortables first li, change the text to that of the 
    //chosen option and append it to the sortable this saves having 
    //html embedded within the js 

    $sortable.children().eq(0) 
         .clone() 
         .text($('#ContentList').val()) 
         .appendTo($sortable); 

    // cache select options 
    var $items = $('#ContentList').children(); 

    //remove selected item 
    $items.filter(':selected').remove() 

    //set first option to be selected 
    $list.eq(0).attr('selected', true); 
});