Reputation: 181
I have this line in a view of an MVC site which is within a ForEach loop that iterates over a set of times in a Model passed to the view.
<li data-cft-times="true" class="selectedList">@times.time
<input type="checkbox" class="ficoEntry" checked="checked" data-cft-time="true" />
</li>
Then in my Jquery I am trying to see if the user has changed the checkboxes to deselect a time and I currently have this but it returns all the times not just the ones that still have the checkbox as ticked.
if ($('input[data-cft-time]').attr("checked") == "checked") {
//do stuff...
}
Can anyone see where I am going wrong as I have tried lots and seem to be getting nowhere but I know it's a simple issue that has me stuck...
Thanks.
EDIT...
I have tried both the posts below and neither work it looks like when I uncheck the checkbox the DOM is not updating as the HTML code using F12 still shows the following
<input type="checkbox" class="ficoEntry" checked="checked" data-cft-time="true" />
Am I missing something here....
MORE INFO..
<ul>
<li class="selectedList" data-cft-times="true">2300
<input class="ficoEntry" type="checkbox" checked="checked" data-cft-time="true">
</li>
<li class="selectedList" data-cft-times="true">0000
<input class="ficoEntry" type="checkbox" checked="checked" data-cft-time="true">
</li>
<li class="selectedList" data-cft-times="true">0100
<input class="ficoEntry" type="checkbox" checked="checked" data-cft-time="true">
</li>
<li class="selectedList" data-cft-times="true">0200
<input class="ficoEntry" type="checkbox" checked="checked" data-cft-time="true">
</li>
</ul>
And here is the current JQuery.
$("li[data-cft-times]").each(function (index) {
text = $(this).text().slice(0, 4);
if ($('input[data-cft-time]').is(':checked')) {
time += "{\"time\":\"" + text + "\"},";
}
});
Upvotes: 0
Views: 282
Reputation: 36794
You can use the :checked
selector then check the length of the collection, which will return 1 if your checkbox is checked:
if ($('input[data-cft-time]:checked').length)
//do stuff...
}
Upvotes: 1