如何根据jQuery中的值检查复选框

发布于 2025-02-06 15:50:56 字数 2313 浏览 2 评论 0原文

我需要根据数据值对我的复选框进行检查状态。到目前为止,我正在使用以下方法。

$(document).ready(function(){  
      $('#add').click(function(){  
           $('#insert').val("Insert");  
           $('#update_form')[0].reset();  
      });  
      $(document).on('click', '.update_data', function(){ 
           var row_id = $(this).attr("id");  
           
           $.ajax({  
                url:"./project/userdetail.php",  
                method:"POST",  
                data:{row_id:row_id},  
                dataType:"json", 
                success:function(data){  
                      ...........
                     if(data.accesslocations=='ABC,DEF,GHI'){
                      document.getElementById("check1").checked = true;
                      document.getElementById("check2").checked = true;
                      document.getElementById("check3").checked = true;
                      
                     }

                     if(data.accesslocations=='ABC,GHI'){
                      document.getElementById("check1").checked = true;
                      document.getElementById("check3").checked = true;
                      
                     }
                     if(data.accesslocations=='ABC'){
                      document.getElementById("check1").checked = true;
                     }

                    
                     
                     
                     ...........

                 $('#employee_id_update').val(data.row_id);  
                 $('#insert').val("Update");
                 $('#new_data_Modal').modal('show'); 
                        
                      
                }, 
                error: function(req, status, error) {
               alert(req.responseText);      
                }   
           });  
      });  

我的目的是检查data.AccessLocations并检查它是否包含abcabc,def同样,

If ABC contains need to check1 status as checked.
If DEF contains need to check2 status as checked.
If GHI contains need to check3 status as checked.

我也尝试了下面的方法。但是然后我无法打开对话框,new_data_modal

               if(strstr(data.accesslocations, "ABC")){
                  document.getElementById("check1").checked = true;
                 }

I need to make checked status of my checkbox according to a data value. Up to now I am using below method.

$(document).ready(function(){  
      $('#add').click(function(){  
           $('#insert').val("Insert");  
           $('#update_form')[0].reset();  
      });  
      $(document).on('click', '.update_data', function(){ 
           var row_id = $(this).attr("id");  
           
           $.ajax({  
                url:"./project/userdetail.php",  
                method:"POST",  
                data:{row_id:row_id},  
                dataType:"json", 
                success:function(data){  
                      ...........
                     if(data.accesslocations=='ABC,DEF,GHI'){
                      document.getElementById("check1").checked = true;
                      document.getElementById("check2").checked = true;
                      document.getElementById("check3").checked = true;
                      
                     }

                     if(data.accesslocations=='ABC,GHI'){
                      document.getElementById("check1").checked = true;
                      document.getElementById("check3").checked = true;
                      
                     }
                     if(data.accesslocations=='ABC'){
                      document.getElementById("check1").checked = true;
                     }

                    
                     
                     
                     ...........

                 $('#employee_id_update').val(data.row_id);  
                 $('#insert').val("Update");
                 $('#new_data_Modal').modal('show'); 
                        
                      
                }, 
                error: function(req, status, error) {
               alert(req.responseText);      
                }   
           });  
      });  

My intention is to check the data.accesslocations and check whether it contains ABC or ABC,DEF likewise,

If ABC contains need to check1 status as checked.
If DEF contains need to check2 status as checked.
If GHI contains need to check3 status as checked.

I have tried below method as well. But then I could not open my dialogbox, new_data_Modal.

               if(strstr(data.accesslocations, "ABC")){
                  document.getElementById("check1").checked = true;
                 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2025-02-13 15:50:56
const handleFoobarCheckboxes = (str) => {
  const values = str.split(",");
  document.querySelectorAll('[name="foobar[]"]').forEach(elCkb => {
    elCkb.checked = values.includes(elCkb.value);
  });
};

handleFoobarCheckboxes("GHI,ABC");
<label><input name="foobar[]" type="checkbox" value="ABC"> ABC</label>
<label><input name="foobar[]" type="checkbox" value="DEF"> DEF</label>
<label><input name="foobar[]" type="checkbox" value="GHI"> GHI</label>

从上面的示例中可以看到,值的顺序将无关紧要,您的脚本仍然可以正常工作。

const handleFoobarCheckboxes = (str) => {
  const values = str.split(",");
  document.querySelectorAll('[name="foobar[]"]').forEach(elCkb => {
    elCkb.checked = values.includes(elCkb.value);
  });
};

handleFoobarCheckboxes("GHI,ABC");
<label><input name="foobar[]" type="checkbox" value="ABC"> ABC</label>
<label><input name="foobar[]" type="checkbox" value="DEF"> DEF</label>
<label><input name="foobar[]" type="checkbox" value="GHI"> GHI</label>

as you can see from the example above, the order of values will not matter and your script will still work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文