0

我在页面中显示了一个产品列表,并且在每个产品记录之前都有一个复选框,如下所示

<script>
      $(document).ready(function(){
            $( "#process_button" ).click(function() {
                var hello = $('.css-checkbox_latest')
                console.log(hello);

                $('.css-checkbox_latest').each(function(index){
                  console.log(index);
                });

            });

           });

    </script>


<table>
 {% for product in list_of_products%} 
  <tr>
    <td>
       <input id="{{product.id}}" class="css-checkbox_latest" type="checkbox" />   
    </td>
    <td>
       {{product.name}}
    </td>
    <td>
       {{product.price}}
    </td>
  </tr>
 {% endfor %}
</table>

<input id="process_button" name="" type="button" class="btn btn-large addp_btn"  value="Process">

并有一个如上的按钮,

1. so when i clicked on the `button`, i should check whether any checkbox fields are `checked`, if they are checked i should fetch the `id attribute` values from the checkbox and make them as a list or tuple or an array of values
2. I had a django function, that accepts this values and do some processing based on the values, so how to call a url(using GET, something liek that) with the above values from the above jquery

从上面的 jquery 中,我可以通过使用一个类来获取复选框元素,但是如果选中了复选框,我需要循环并获取 id 值,那么如何在 jquery 中执行此操作?

4

1 回答 1

1

尝试

jQuery(function ($) {
    $("#process_button").click(function () {
        //create an array of checked checkbox ids
        var ids = $('.css-checkbox_latest:checked').map(function (index) {
            return this.id;
        }).get();

        //if nothing is selected alert the user and stop further processing
        if (!ids.length) {
            alert('nothing is selected')
            return;
        }

        //send a request to server using an ajax POST request, it will contains an array of parameters called ids
        $.ajax({
            url: '<url>',
            type: 'POST',
            data: {
                ids: ids
            }
        })
    });
});
于 2013-11-01T15:13:57.507 回答