jQuery 获取ul中active li的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22891226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of active li in ul
提问by user2274191
I am trying to get the active li from a ul in jqvalue but all i am getting is null. I've also tried to write the value of the ul using console dir (to try and simulate php's var_dump
but nothing is happening there either
我试图从 jqvalue 中的 ul 获取活动 li,但我得到的只是空值。我还尝试使用控制台目录写入 ul 的值(尝试模拟 php,var_dump
但那里也没有发生任何事情
Here is my jfidde: http://jsfiddle.net/#&togetherjs=MDbkUMciB7
这是我的 jfidde:http: //jsfiddle.net/#&togetherjs=MDbkUMciB7
Here is the code
这是代码
function monthly_payment(){
var interest = $('ul.credit').find('li.active').data('interest');
console.dir($('ul.credit'));
alert(interest);
// var iinterest = interest_rate / 1200;
}
<ul id="credit">
<li interest = "0.056" class="active"><a href="#"><span id="excellent" class="0.056">excellent</span></a></li>
<li interest = "0.099"><a href="#"><span id="good" class="0.099">good</span></a></li> <li interest = "0.169"a href="#"><span id="fair" class="0.169">fair</span></a></li>
<li interest = 0.299><a href="#"><span id="bad" class="0.299">bad</span></a></li>
</ul>
回答by Shaminder S Aujla
Change class to ID i.e li.credit
to li#credit
and interest
to data-interest
将类更改为 ID ie li.credit
toli#credit
和interest
todata-interest
HTML
HTML
<ul id="credit" name="credit">
<li data-interest = "0.056" class="active"><a href="#"><span id="excellent" class="0.056">excellent</span></a></li>
<li data-interest = "0.099"><a href="#"><span id="good" class="0.099">good</span></a></li>
<li data-interest = "0.169"><a href="#"><span id="fair" class="0.169">fair</span></a></li>
<li data-interest = 0.299><a href="#"><span id="bad" class="0.299">bad</span></a></li>
</ul>
JQuery
查询
$(function() {
monthly_payment();
});
function monthly_payment(){
var interest = $('ul#credit').find('li.active').data('interest');
alert(interest);
console.dir($('ul.credit'));
var iinterest = interest_rate / 1200;
payment = principal * interest / (1 - (Math.pow(1/(1 + interest), monthly_period)));
payment = Math.round(payment * 100) / 100;
return payment;
}
回答by Amit Joki
If you want to use data attributes, then prefix it with data-
如果您想使用数据属性,则使用前缀 data-
Your HTML should be:
你的 HTML 应该是:
<ul id="credit">
<li data-interest = "0.056" class="active"><a href="#"><span id="excellent" class="0.056">excellent</span></a></li>
<li data-interest = "0.099"><a href="#"><span id="good" class="0.099">good</span></a></li> <li data-interest = "0.169"a href="#"><span id="fair" class="0.169">fair</span></a></li>
<li data-interest = 0.299><a href="#"><span id="bad" class="0.299">bad</span></a></li>
</ul>
And, as mplungjan, said, class names cannot be started with numbers. It has to start with alphabet.
而且,正如 mplungjan 所说,类名不能以数字开头。它必须以字母开头。
.1{
/* bad */
}
.some1{
/* good */
}
回答by Jaypal
You need to change this line
你需要改变这一行
var interest = $('ul#credit').find('li.active').attr('interest');
you have 'credit' as id in your html.
您的 html 中有“信用”作为 id。