2017-07-29 57 views
3

我有下面的代码所有三个DIV具有相同的类名我想要做的是通过使用jquery索引号访问/检索第二个DIV值第二个DIV的值是3.21,我试过了使用此代码检索div.OddVal.index(2)没有运气任何想法家伙我错了吗?使用索引号检索值jquery

<div class="ThreeWayCol OddsCol1"> 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.37</div> 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.21</div> 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">2.07</div> 
</div> 

回答

5

您已经标记了该问题与jQuery的,所以我相信你正在寻找.eq()

减少匹配的元素集合到一个指定索引处。

console.log($('.OddVal').eq(1).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.37</div> 
 

 
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.21</div> 
 

 
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">2.07</div>

+0

谢谢@Ori Drori,这就是我一直在寻找的,有福的。 – Wilson

+0

活得长久而繁荣:) –

2

eq已经在其他的答案中说明,所以我不会再增加多个方面。 我用n:th选择和each

$(".OddVal").each(function(index) { 
 
    if (index === 1) { 
 
    console.log("Each loop result " + $(this).text()); 
 
    } 
 
}); 
 
console.log("nth slector result " + $(".OddVal:nth-child(2)").text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="ThreeWayCol OddsCol1"> 
 

 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.37</div> 
 

 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.21</div> 
 

 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">2.07</div> 
 

 
</div>

+0

谢谢@Ishan Mahjan这就是我一直在寻找的东西。 – Wilson

1

使用jQuery eq()方法来获取指定索引的值作为

var val = $('.OddVal').eq(your_index_no_here).text();// must be numeric as 1,2,3 
alert(val); 

你可以得到机制的文档JQuery eq()

+0

感谢@RAUSHAN KUMAR这就是我一直在寻找的。 – Wilson

1

你可以看到在这里工作的例子:

演示在这里:https://jsbin.com/rubapoz/edit?html,output

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script> 
 
    $(document).ready(function() { 
 
     $("#btn").click(function() { 
 
     var index = $("#txtIndex").val(); 
 
     alert($('.ThreeWayCol .OddVal').eq(index).text()); 
 
     }); 
 
    }); 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <div class="ThreeWayCol OddsCol1"> 
 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.37</div> 
 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.21</div> 
 
    <div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">2.07</div> 
 
    </div> 
 
    <hr /> 
 
    <input type="text" placeholder="Enter index like: 0,1" id="txtIndex" /> 
 
    <input type="button" value="Get Value" id="btn" /> 
 
</body> 
 

 
</html>

+0

非常有帮助,非常感谢@CKG – Wilson

0

你可以使用https://api.jquery.com/get/

的获得()方法授予访问DOM节点底层每个jQuery对象。如果索引的值超出范围 - 小于元素的负数或等于或大于元素的数量 - 则返回undefined。考虑一个简单的无序列表:

<ul> 
    <li id="foo">foo</li> 
    <li id="bar">bar</li> 
</ul> 

随着指定的索引,获得(索引)检索单个元件:

console.log($("li").get(0)); 

由于索引是基于零的,则返回第一个列表项:

<li id="foo">