获取ul中活动li的值

时间:2014-04-06 07:36:58

标签: jquery

我试图从jqvalue中的ul获取活动li,但我得到的只是null。我也试图用控制台目录来写ul的值(尝试模拟php' s var_dump但是没有发生任何事情

这是我的jfidde:http://jsfiddle.net/#&togetherjs=MDbkUMciB7

这是代码

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>

3 个答案:

答案 0 :(得分:6)

将班级更改为ID,li.credit改为li#creditinterest改为data-interest

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;

}

答案 1 :(得分:1)

如果您想使用数据属性,请在其前面添加data-

您的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>

并且,正如mplungjan所说,类名不能以数字开头。它必须以字母开头。

.1{
 /* bad */
}

.some1{
 /* good */
}

答案 2 :(得分:-1)

您需要更改此行

var interest = $('ul#credit').find('li.active').attr('interest');

你有信用&#39;作为你的HTML中的id。