开发者

how can i link a list element with the select dropdown

I have a list of elements:

<li>A</li>
<li>B</li>

and a dropdown:

<select>    
    <options value="A">A</options> 
    <开发者_如何学编程options value="B">B</options>    
</select>

How can i link both of them together?

I want to change the value of select to the text of the <li> that the user clicks. So if the user clicks <li>A</li>, the <select> value would be set to A.


Try something like this:

$('ul li').click(function() {
  var elem=$(this).text();
  $("select option[value='"+elem+"']").attr('selected', 'selected');
});


$("#list li").click(function(){

var index = $(this).text(); 

$("#dropdown").val(index);

});

Here's one way, though you can change the index variable to suit your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜