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.
精彩评论