Javascript 对所有选中的复选框使用函数
我在为所有选中的复选框使用JavaScript函数时遇到了一些问题。下面是它的外观:Javascript 对所有选中的复选框使用函数,javascript,function,checkbox,selected,Javascript,Function,Checkbox,Selected,我在为所有选中的复选框使用JavaScript函数时遇到了一些问题。下面是它的外观: <script type="text/javascript"> function toggle(source) { checkboxes = document.getElementsByName('checkbox'); for(var i=0, n=checkboxes.length;i<n;i++) { checkboxes[
<script type="text/javascript">
function toggle(source) {
checkboxes = document.getElementsByName('checkbox');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
function backToActive(id){ // for a single item
$.ajax({
type: "POST",
url: '/backtoactive/'+id,
success: function(){
$("#line_"+id).hide(1000);
}
});
}
function massiveBackToActive(){ // for multiple checked items
var pids = checkedProducts.join();
$.ajax({
type: "POST",
data: { ids : pids },
url: '/massivebacktoactive/',
success: function(){
window.location.href = window.location.href;
}
});
}
function checkProduct(id){
var a = checkedProducts.indexOf(id);
if( a >= 0 ){
checkedProducts.splice(a,1);
}
else{
checkedProducts.push(id);
}
}
</script>
函数切换(源){
checkbox=document.getElementsByName('checkbox');
对于(变量i=0,n=checkbox.length;i=0){
检查产品.拼接(a,1);
}
否则{
检查产品。推送(id);
}
}
那么我有这个,
<tr>
<th>
<input type="checkbox" onClick="toggle(this)" />
</th>
<th>img</th>
<th>Name</th>
</tr>
<tr id="line_<?php echo $item->productid;?>">
<td>
<input type="checkbox" name="checkbox" onchange="checkProduct(<?php echo $item->productid;?>)" />
</td>
<td><a href="/item/?id=<?php echo $item->productid;?>" class="bold"><img src="<?php echo $item->imgurl;?>" /></a></td>
<td><a target="_new" href="<?php echo $item->url;?>" class="bold"><?php echo $item->name;?></a></td>
</tr>
img
名称
主要问题是以编程方式更改选中的属性值不会触发更改事件,因此当您使用全选时,checkedProducts
数组不会得到更新
但是,由于您使用的是jQuery,请尝试一个完整的jQuery解决方案,所以请像这样更改标记
<input type="checkbox" id="checkboxall" />
你好非常感谢您的回复,但遗憾的是,这并不能解决问题。事实上,它甚至不能选择所有项目。如果您还有其他建议,我很乐意尝试。@Jocker有一些语法错误。。。。修正了。。。。现在试试-不。。。对不起,但还是不起作用。。。我可以在fiddle中看到它正在工作,但出于某种原因,它无法运行我的代码。我绝对相信我没有犯任何错误。使用我以前使用的函数toggle(),复选框的切换是有效的。@Jocker在页面加载后是否动态创建复选框这绝对是完美的,伙计!多谢各位。因为这是我在这里的第一个问题,我真的不知道这是如何工作的,所以如果我有任何方法给你一些反馈或类似的东西,请让我知道。
<input type="checkbox" name="checkbox" data-id="<?php echo $item->productid;?>" />
$('#checkboxall').change(function () {
$('input[name="checkbox"]')[this.checked ? 'not' : 'filter'](':checked').prop('checked', this.checked).change();
})
$('input[name="checkbox"]').change(function () {
var id = $(this).data('id')
if (this.checked) {
checkedProducts.push(id);
} else {
var index = checkedProducts.indexOf(id);
if (index > -1) {
checkedProducts.splice(index, 1);
}
}
console.log(checkedProducts)
})
function backToActive(id) { // for a single item
$.ajax({
type: "POST",
url: '/backtoactive/' + id,
success: function () {
$("#line_" + id).hide(1000);
}
});
}
function massiveBackToActive() { // for multiple checked items
var pids = checkedProducts.join();
$.ajax({
type: "POST",
data: {
ids: pids
},
url: '/massivebacktoactive/',
success: function () {
window.location.href = window.location.href;
}
});
}