Вопрос

<div class="posts">
<input id="chkbox_0" type="checkbox" value="chck_0" name="list" style="float:left;height:95px; width:15px;">
<div class="title box " onclick="thisimg(0,252250)">
<div class="click" style="float:right;">
Clicks
<br>
<div id="count_0"> 0 </div>
</div>
</div>
</div>

<div class="posts">
<input id="chkbox_1" type="checkbox" value="chck_1" name="list" style="float:left;height:95px; width:15px;">
<div class="title box " onclick="thisimg(0,252250)">
<div class="click" style="float:right;">
Clicks
<br>
<div id="count_1"> 0 </div>
</div>
</div>
</div>

<div class="posts">
<input id="chkbox_2" type="checkbox" value="chck_2" name="list" style="float:left;height:95px; width:15px;">
<div class="title box " onclick="thisimg(0,252250)">
<div class="click" style="float:right;">
Clicks
<br>
<div id="count_2"> 6 </div>
</div>
</div>
</div>

<div class="posts">
    <input id="chkbox_3" type="checkbox" value="chck_3" name="list" style="float:left;height:95px; width:15px;">
<div class="title box " onclick="thisimg(0,252250)">
<div class="click" style="float:right;">
Clicks
<br>
<div id="count_3"> 0 </div>
</div>
</div>
</div>

I've been working on this for 7 days with no success so I'm assuming I'm asking the question incorrectly. So what I'm trying to do is check the box that corresponds to the click elemment index that is 0. NOT THE INDEX 0 BUT THE NUMBER ZERO. I have 4 snippets of identical code with the exception of the indexes. At times the number of clicks are mostly ZERO which is the important thing. I want the checkbox that is indexed the same as the clicks that are ZERO. This is what I tried:

var allZeros = $("[id^=count]:contains(0)").css('color', 'red').each(function(i) {
  $("input:checkbox").trigger('click');
});

This checks all of the boxes.

I've also tried

$("input:checkbox").eq(i).trigger('click');
$("input:checkbox").trigger('click').attr('id');  

both of which use the allZeros index and checks that number of boxes. For instance in the example I have 3 allZeros so it checkboxes the first 3 but what it should do is checkbox the first 2 ZEROS, skip the one with 6 clicks and checkbox the 3rd ZERO.

You can check the page here: http://www.zyngapokerchipsfree.com/ it has between 60 and 70 items on the page. I hope this is much clearer. Thanks for your help.

Это было полезно?

Решение

You can use IDs of the matched elements.

var $c = $("input[type=checkbox]");

$("div[id^=count]:contains(0)").css('color', 'red').each(function(i) {
    var id = this.id.replace('count', 'chkbox');
    $c.filter('[id="'+ id +'"]').prop('checked', true);
});

In case that you want to select elements that their text content is just 0 and not 10, 20, ... you can use filter method:

$("div[id^=count]").filter(function(){
   return $.trim( $(this).text() ) === '0';
}).css('color', 'red').each(function(i) {
    var id = this.id.replace('count', 'chkbox');
    $c.filter('[id="'+ id +'"]').prop('checked', true);
});

Другие советы

To check a checkbox you need to use

$('checkbox').prop('checked', 'checked')

In your case

var allZeros = $("div[id^=count]:contains(0)").css('color', 'red').each(function(){
    var id = $(this).attr('id').substring(6);
    $('#chkbox_'+id).prop('checked', 'checked')
})

Demo: Fiddle

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top