动态填充复选框,并在勾选函数调用服务器端

Populate checkbox dynamically and on tick function calls server side

本文关键字:函数调用 服务器端 填充 复选框 动态      更新时间:2023-09-26

我需要创建一个带有动态填充行复选框的表。复选框列表将根据他们所在的公司而更改

e。g:第一家公司:Apple friends

 Name     email        iPhone   iPad  macbook  macpro 
 john  jo@mail.com      ckb      ckb     ckb      ckb
 Mel   mel@mail.com     ckb      ckb     ckb      ckb 

第二家公司:Mirco Friends

 Name     email        windows   office 
 jav   jav@mail.com      ckb      ckb    
 Kel   Kel@mail.com      ckb      ckb   

如果这是可行的,我如何去创建一个方法来捕获任何更改事件的复选框?

一个普通的复选框更改功能:

$('#checkbox1').change(function())

但是我们如何为动态定义的复选框编写代码或id呢?

我知道这看起来像两个问题,但是第二个问题和第一个问题是联系在一起的。

感谢您的帮助

大致思路是,循环遍历每一个选中的复选框,然后获取复选框的id,如果你能获得id,我相信你可以很容易地使用你得到的id做任何你想做的事情

$(':checked').each(function(){
    var $this = $(this);
    console.log($this.attr('id'));
});

$(':checkbox').change(function(){
    var $this = $(this);
    console.log($this.attr(id)); // u get the id here if any checkbox is tick/untick
    if ($this.prop("checked")){
        //do something when it is checked
    }else{
        //do something when it is unchecked
    }
});

你可以试试

$(document).on('change', '#checkbox1', function() {
alert('alerted');
});