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

javascript - jquery - change a drop down menu to a list - Stack Overflow

programmeradmin1浏览0评论

Is there a way to convert a drop down menu to be a list using jquery... so:

<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>

to

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>

Thanks

Is there a way to convert a drop down menu to be a list using jquery... so:

<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>

to

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>

Thanks

Share Improve this question asked Nov 2, 2010 at 16:57 TomTom 13k50 gold badges153 silver badges247 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

Just iterate over the <option> elements and create a corresponding <li> for each, then add them to a <ul>, like this: (where #menu is the ID of your drop-down)

var list = $('<ul>');
$('#menu option').each(function() {
  $('<li>').text($(this).text()).appendTo(list);
});
list.appendTo($('body'));

Working example on JSBin

Initially wrap select in span as shown below -

Try this -

  <span id="replace">
    <select id="test">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
  </span>

var html = '<ul>';
$('#test option').each(function(){
html += '<li>'+$(this).text()+'</li>';
});
html += '</ul>';

$('#replace').html(html);

Magic!!!

If you get the data by any means

replace the dropdown by .html() or .text().

populate the ul and li and concatinate into a variable like

var newcontent = "<ul> 
<li>1</li> 
<li>2</li> 
<li>3</li> 
</ul> "

i assume the select box is in a div with id "content"

replace content by ('#content').html(newcontent )

or text.

NB. You should get the data and create the ul li . then you can replace

Tag names are immutable in DOM elements. If possible you should consider including both in the source code and then use toggle() to switch

发布评论

评论列表(0)

  1. 暂无评论