2012-07-20 50 views
1

我有下表。我也有這個數組,其值與行[52,24,12]的data-id屬性相對應。對於每個數組值,表中只會存在一行。我想爲數組中存在的每一行增加count列。例如,12會變爲13,32會變爲33,6會變爲7.jQuery解決方案是首選,但是,本地JavaScript解決方案就足夠了。謝謝通過給定值的屬性選擇元素

<table> 
<thead> 
    <tr><td>count</td></tr> 
</thead> 
<tbody> 
    <tr data-id="24"><td>32</td></tr> 
    <tr data-id="52"><td>12</td></tr> 
    <tr data-id="42"><td>4</td></tr> 
    <tr data-id="84"><td>2</td></tr> 
    <tr data-id="12"><td>6</td></tr> 
</tbody> 
</table> 
+0

你可以發佈你正在使用實際創建數組的代碼? – 2012-07-20 16:59:53

+0

var myArray = $('#list input:checked'); – user1032531 2012-07-20 17:02:16

+0

這些答案的每個人都很棒!特勒的第二個答案似乎是最簡單的,但是,不知道它是否最有效。無論如何,我必須選擇一些東西,所以......謝謝大家! – user1032531 2012-07-20 17:25:52

回答

1

試試這個(我用的lib從underscorejs.org)

_.each([52,24,12], function (item) { 
    var td = $('tr[data-id=' + item + '] td'); 
    td.text(parseInt(td.text()) + 1); 
}); 

或W/O下劃線:

var a = [52,24,12]; 
for (var i = 0; i < a.length; ++i) { 
    var td = $('tr[data-id=' + a[i] + '] td'); 
    td.text(parseInt(td.text()) + 1); 
} 

http://jsfiddle.net/ACJ9r/

1

Something like this應該做的伎倆

var indices = [52, 24, 12]; 

//loop through each array entry 
jQuery.each(indices, function(i, val) { 

    var tr, value; 

    //get the tr with the corresponding data-id, cache for reuse 
    td = $('tr[data-id="' + val + '"] td'); 

    //just trying to be verbose 
    value = td.text();   //get the text 
    value = parseInt(value, 10); //parse the number off it 
    value++;      //increment 

    //put the incremented values back 
    td.text(value); 

});​ 
1

嘗試:

HTML

<table> 
<thead> 
    <tr><td>count</td></tr> 
</thead> 
<tbody> 
    <tr data-id="24"><td>32</td></tr> 
    <tr data-id="52"><td>12</td></tr> 
    <tr data-id="42"><td>4</td></tr> 
    <tr data-id="84"><td>2</td></tr> 
    <tr data-id="12"><td>6</td></tr> 
</tbody> 
</table>​ 

jQuery的

indices = [52, 24, 12]; 

$('tr[data-id]').each(function() { 
    if (indices.indexOf($(this).data('id')) == -1) { 
     return; 
    } 
    var td = parseInt($(this).find('td').html()); 
    $(this).find('td').html(td + 1); 
}); 

的jsfiddlehttp://jsfiddle.net/Xc7JC/1/

享受和好運氣!

+0

這不僅適用於ID數組中的數字。這對所有具有id的行都有效。 – jfriend00 2012-07-20 17:06:07

+0

答覆已更新。 – 2012-07-20 17:10:48

1

你可以像這樣做,只對陣列中存在ID的行進行操作。

這比使用多個jQuery調用來查找數組中的每個id的解決方案可能更有效,因爲這隻需要遍歷一次行,而那些必須遍歷錶行N次。

<table id="myTable"> 
<thead> 
    <tr><td>count</td></tr> 
</thead> 
<tbody> 
    <tr data-id="24"><td>32</td></tr> 
    <tr data-id="52"><td>12</td></tr> 
    <tr data-id="42"><td>4</td></tr> 
    <tr data-id="84"><td>2</td></tr> 
    <tr data-id="12"><td>6</td></tr> 
</tbody> 
</table> 

var rowList = [52, 24, 12]; 

$("#myTable tr").each(function() { 
    var id = $(this).data("id"); 
    if (id && $.inArray(id, rowList) != -1) { 
     var cell = $(this).find("td"); 
     cell.text(parseInt(cell.text(), 10) + 1); 
    } 
}); 
+0

似乎不起作用。 http://jsfiddle.net/BKDLZ/ – user1032531 2012-07-20 17:16:41

+0

@ user1032531 - 修復了幾個拼寫錯誤,並更新了我的答案。現在的作品:http://jsfiddle.net/jfriend00/t2YyP/ – jfriend00 2012-07-20 17:19:24

+0

是的,在契約中工作。哇,多好的答案! – user1032531 2012-07-20 17:19:57

1

你可以不喜歡它這,

Live Demo

arr = [52,24,12]; 
$('tr').each(function(){ 
    if($.inArray($(this).data('id'), arr) > -1) 
     $(this).children('td').text(+$(this).children('td').text()+1); 
});​ 
相關問題