2017-07-29 59 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">