Validate at least One check box is checked in django html template via Ajax

I am showing pagination gird on django templates,

{% for result in page.object_list %}
<tr>
<td style="border:0;border-bottom:1px solid #eeeeee;">
<input type="checkbox" id="{{result.object.user_id}}" name="selectedvalues[]" value="{{ result.object.user_id }}">
            </td>
<td>.....</td>
<tr>
{% endfor %}

and there is a button at the end of this to delete the users. I want to validate that on delete button press it should validate that at least one user is selected via check box.

$("#delete").click(function(){
    var $checkboxes $('# td input[type="checkbox"]');
    $('.checkbox input').each(function(index) {
        if($(this).is(':checked')) {
            break;
        } else {
            $(this).attr('value', 'False');
        }
      alert('You must select at lease one user');
    });

    });

but it is not working.....

Answers


$("#delete").click(function() {
    if ($('input[type="checkbox"]:checked').length > 0) {
        alert("Checked");
    } else {
        alert("Not checked");
    }
});

If you have other checkboxes on page then add a class to your <input> and select items in JS by this class:

<input type="checkbox" class="deletechk" name="selectedvalues[]"
                                         value="{{ result.object.user_id }}">

And in Javascript code:

if ($('.deletechk:checked').length > 0) {
    ...
}

Need Your Help

how to use boost::algorithm::join on tuples?

c++ algorithm boost c++11

boost::algorithm::join provides a convenient join on std::vector&lt;std::string&gt;.

Remove extra lines in a text file

java file line

I would like to remove an extra line at the front of the text file that's being created for some reason. Here's the code for what I'm doing:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.