如何获取在按钮事件上被单击的列表项的ID? [英] How to get the ID of the listitem being clicked on button event?
本文介绍了如何获取在按钮事件上被单击的列表项的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有锚标签和一个按钮的列表项,单击按钮后我需要选择li.我如何使用Jquery.我不知道如何获取li的单击事件.我的html代码是:
I have list item with anchor tag and a button, on click of the button i need to get the li selected. how do i do using Jquery. I am not aware as how do I get the clicked event of li . my html code is as :
<ul id="nav">
<li><a href="#" rel="css/default.css" id="default" >
<div class="r1"></div>
</a></li>
<li><a href="#" rel="css/winter.css" id="winter">
<div class="r2"></div>
</a></li>
<li><a href="#" rel="css/spring.css" id="spring">
<div class="r3"></div>
</a></li>
<li><a href="#" rel="css/summer.css" id="summer">
<div class="r4"></div>
</a></li>
<li><a href="#" rel="css/autumn.css" id="autumn">
<div class="r5"></div>
</a></li>
</ul>
<a href="#" class="sbutton_in" style="margin: 25px auto auto 80px;" id="btnapply">Apply</a> </div>
$("#btnapply").click(function(){
//code to get the id of the selected li
});
推荐答案
您需要为li应用click事件,
You need to apply click event for li,
$('ul#nav li').on('click',function() {
$('ul#nav li').removeClass('active');
$(this).addClass('active');
});
然后
$("#btnapply").click(function(){
var ID = $('ul#nav li.active').attr('id');
});
在ID中,您将获得所选li的ID.
Here in ID you will get ID of selected li.
您需要为每个li赋予唯一的ID.
You need to give unique ID to each li.
这篇关于如何获取在按钮事件上被单击的列表项的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文