最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Append UL with LI with value from dropdownlist on button click - Stack Overflow

programmeradmin3浏览0评论

I have a dropdownlist:

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

I have a sortable list:

<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>

I have a button:

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

And the following script:

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


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

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

            });

        });

    </script>

When a user select something in the dropdown list and clicks Add I want that to get sent to the sortable list as an <li> with the class attribute etc, the dropdown to now show the 'Please Select' option.

Not sure the best approach to take. Thanks

I have a dropdownlist:

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

I have a sortable list:

<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>

I have a button:

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

And the following script:

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


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

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

            });

        });

    </script>

When a user select something in the dropdown list and clicks Add I want that to get sent to the sortable list as an <li> with the class attribute etc, the dropdown to now show the 'Please Select' option.

Not sure the best approach to take. Thanks

Share Improve this question asked Jul 13, 2009 at 11:06 JonJon 40.1k87 gold badges242 silver badges393 bronze badges
Add a ment  | 

2 Answers 2

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

should do the trick... (:

working demo 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); 
}); 
发布评论

评论列表(0)

  1. 暂无评论