复选框(iswitch)在html表的多行

Checkbox (iswitch) in html table with multiple rows

本文关键字:html iswitch 复选框      更新时间:2023-09-26

这是HTML代码(表代码的一部分):

<td class="center">
    <?php if ($row['datum_afhaling']) { ?>
            <input class="active iswitch iswitch-success" type="checkbox" cdid="<?php echo $row['cdid']; ?>" value="<?php echo $row['id']; ?>" checked></td>
        <?php } else { ?>
            <input class="active iswitch iswitch-success" type="checkbox" cdid="<?php echo $row['cdid']; ?>" value="<?php echo $row['id']; ?>"></td>
    <?php } ?>
</td>

和javascript代码:

$("input.active").click(function() {
    var check_active = $(this).is(':checked') ? 'ja' : 'nee';
    var check_id = $(this).attr('value');
    var cd_id = $(this).attr('cdid');
    $.ajax({
        type: "POST",
        url: "cudi-afhalen-status-ajax.php",
        data: {id: check_id, active: check_active, cdid: cd_id},
        success: function(){
            alert ('id: '+check_id+' & active: '+check_active+' & cdid: '+cd_id);
            location.href='cudi-bestellingen-overzicht.php';
        }
    });
    return true;
});

如果我将复选框切换到另一边,我创建了一个带有一些参数的alertbox。到目前为止,这工作得很好,但是代码给了我15个alertboxen,因为我的表中有15行。所有告警框中的值(params)是相同的。我切换了1个checkbox-iswitch,所以我将只有1个alertbox和参数,而不是所有其他14个框。我不知道为什么会这样?

编辑我发现这里的代码出了什么问题。javascript代码在while循环中。所以我把它替换到循环外,一切正常

试试下面的代码

$("input.active").click(function() {
var check_active = $(this).is(':checked') ? 'ja' : 'nee';
var check_id = $(this).attr('value');
var cd_id = $(this).attr('cdid');
var al = 1;
$.ajax({
    type: "POST",
    url: "cudi-afhalen-status-ajax.php",
    data: {id: check_id, active: check_active, cdid: cd_id},
    success: function(){
        if(al==1){
        alert ('id: '+check_id+' & active: '+check_active+' & cdid: '+cd_id);
         al=0;
         }
          location.href='cudi-bestellingen-overzicht.php';
    }
});
return true;
});