获取ul中活性li的值

时间:2022-11-27 21:38:23

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,但我得到的是空的。我还尝试使用console dir编写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>

3 个解决方案

#1


6  

Change class to ID i.e li.credit to li#credit and interest to data-interest

将类更改为ID i。e。贷款给#贷款,利息给数据利息

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

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;

}

#2


1  

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 */
}

#3


-1  

You need to change this line

你需要改变这条线

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

you have 'credit' as id in your html.

在html中,“credit”作为id。

#1


6  

Change class to ID i.e li.credit to li#credit and interest to data-interest

将类更改为ID i。e。贷款给#贷款,利息给数据利息

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

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;

}

#2


1  

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 */
}

#3


-1  

You need to change this line

你需要改变这条线

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

you have 'credit' as id in your html.

在html中,“credit”作为id。