开发者

how can i link a list element with the select dropdown

开发者 https://www.devze.com 2023-04-08 16:49 出处:网络
I have a list of elements: <li>A</li> <li>B</li> and a dropdown: <select> <options value=\"A\">A</options>

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

精彩评论

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

关注公众号