复选框必须保持选中状态

时间:2020-03-31 16:54:36

标签: javascript

我希望我的复选框在使用搜索过滤器时保持选中状态。每次在搜索栏中输入内容时,都会指定要显示哪个传感器,这会导致复选框(与传感器链接)消失并根据在搜索栏中输入的内容重新出现。每次程序重新运行时,我都会检查哪些传感器已选中/未选中。对于未检查的对象,被检查的对象被推送到数组,反之亦然(除非它们被删除)。

我已经创建了一些已经可以部分使用的东西,它确实处于选中状态,并且可以用于多个选中的复选框,除非当我取消选中它时,每次都无法正常工作。这是由于在搜索栏中输入内容时不断变化所致。

我当前使用的方法:

if(pool.length != 0){
  load_data();
}

function load_data(query)
{
  //Part where I delete the SensorIDs from the array based on which are unchecked
  $.each($("input[name='sensorcheckbox']"), function(){
    sensors.splice(0, sensors.length);
    if($(this).prop("checked") == false){
              var index = sensors.indexOf($(this).val());
              if (index > -1){
                sensors.splice(index, 1);
              }
            }
  });

  //Part where I push the SensorIDs in the array based on which are checked
  $.each($("input[name='sensorcheckbox']"), function(){
            if($(this).prop("checked") == true){
                  if(sensors.includes($(this).val())){
                  }else{
                    sensors.push($(this).val());
                  }
            }

    });

    console.log(sensors);
  $.ajax({
  url:"fetchpoolsensors.php",
  method:"POST",
  data:{query:query, pool:pool, sensors:sensors},
  success:function(data)
  {
    console.log(pool);

   $('#result3').html(data);
  }
 });
}
if(pool.length != 0){
$('#search3').keyup(function(){
 var search = $(this).val();
 if(search != '')
 {
  load_data(search);
 }
 else
 {
  load_data();
 }
});
}
})
});
</script>

0 个答案:

没有答案