[英]How to check the checkbox field checked and fetch the attribute id value from the element in jquery and 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中,我可以通过使用类来获取checkbox元素,但是如果需要选中checkbox,我需要循环遍历并获取id值,那么如何在jquery中做到这一点?
尝试
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
}
})
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.