2013-11-01 57 views
0

我在一個頁面中顯示的產品清單,並有各自前的複選框,每一個產品記錄如下如何檢查複選框現場檢查,並取值從jQuery的元素屬性ID和Django的

<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中做到這一點?

回答

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 
      } 
     }) 
    }); 
}); 
+0

@ArunThanks,可以抽到在我看來/函數的值當我爲'IDS []'與功能鍵打印GET值(字典),但是當我嘗試獲取從字典中的關鍵我只得到起始價值,所以y是這樣嗎? –

+0

@shivakrishna不知道如何在django中處理參數數組 –

相關問題