How to send id of a checked checkboxes which contains categories to ajax and show subcategories related to that checked checkbox?

huangapple go评论74阅读模式
英文:

How to send id of a checked checkboxes which contains categories to ajax and show subcategories related to that checked checkbox?

问题

I am successfully send checkbox value which is id of a category when checkbox is checked and show subcategories on the same page related to selected subcategory but I have a problem when click on multiple categories and again unchecked one of the category all subcategories goes undisplay. I want to undisplay the subcategory of only unchecked category when I check and uncheck the values anytime. here is my code hope you get my point thank you

Ajax

<script>
function showUser(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {

        document.getElementById("txtHint").innerHTML += this.responseText;
    
      }
    };

    
    xmlhttp.open("POST","getsubcat.php?q="+str,true);
    xmlhttp.send();
    
  }
}
</script>

JQuery

<script>
$(function() {
    $('input[type=checkbox]').on('change', function() {
    if($(this).is(':checked')) {
        showUser($(this).val());
    }   
    else {       
        showUser("");
    }
});
</script>

Category Checkboxes

<?php          
foreach($cat as $row) {
?> 
<input class="messageCheckbox" type="checkbox" id="check" value="<?php echo $row["id"]; ?>">
<label for="check"><?php echo $row["name"]; ?></label>
<?php
}
?>
<br>
<div id="txtHint"><b></b></div>

getsubcat.php

<?php
$q = intval($_GET['q']);
$result = $link->query("SELECT * FROM subcat WHERE cat_id = '".$q."'");
?>
<div class="checkbox">
<?php
while($row = mysqli_fetch_array($result)) {
?>
<input type="checkbox" id="sub_cat" name="sub_cat" value="<?php echo $row['subcat']; ?>">
<label for="sub_cat"> <?php echo $row['subcat']; ?></label><br>
<?php 
}
?>
英文:

I am successfully send checkbox value which is id of a category when checkbox is checked and show subcategories on thee same page related to selected subcategory but I have a problem when click on multiple categories and again unchecked one of the category all subcategories goes un display. I want to un display the subcategory of only unchecked category when I check and uncheck the values anytime. here is my code hope you get my point thank you

**Ajax **

&lt;script&gt;
function showUser(str) {
  if (str == &quot;&quot;) {
    document.getElementById(&quot;txtHint&quot;).innerHTML = &quot;&quot;;
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 &amp;&amp; this.status == 200) {

        document.getElementById(&quot;txtHint&quot;).innerHTML += this.responseText;
    
      }
    };

    
    xmlhttp.open(&quot;POST&quot;,&quot;getsubcat.php?q=&quot;+str,true);
    xmlhttp.send();
    
  }
}
&lt;/script&gt;

J query

 &lt;script&gt;
         
          $(function() {
    //code here
    $(&#39;input[type=checkbox]&#39;).on(&#39;change&#39;, function() {
    if($(this).is(&#39;:checked&#39;)) {
        showUser($(this).val());
    }
    
    
    else{
        
        showUser(&quot;&quot;);
    }
});
          
         
});
          
      &lt;/script&gt;

category checkboxes

  &lt;?php
          
          
            foreach($cat as $row)
            {
                
            ?&gt; 
           &lt;input class = &quot;messageCheckbox&quot; type = &quot;checkbox&quot; id = &quot;check&quot; value = &quot;&lt;?php echo $row[&quot;id&quot;]; ?&gt;&quot;&gt;
           &lt;label for = &quot;check&quot;&gt;&lt;?php echo $row[&quot;name&quot;]; ?&gt;&lt;/label&gt;
           
           
           &lt;?php
            }
           ?&gt;
    
    &lt;br&gt;
    
    &lt;div id=&quot;txtHint&quot;&gt;&lt;b&gt;&lt;/b&gt;&lt;/div&gt;

getsubcat.php

&lt;?php
$q = intval($_GET[&#39;q&#39;]);


$result = $link-&gt;query(&quot;SELECT *
FROM subcat WHERE cat_id = &#39;&quot;.$q.&quot;&#39;&quot;);

?&gt;
&lt;div class = &quot;checkbox&quot;&gt;
    
&lt;?php
while($row = mysqli_fetch_array($result)) {
    
?&gt;

&lt;input type=&quot;checkbox&quot; id=&quot;sub_cat&quot; name=&quot;sub_cat&quot; value=&quot;&lt;?php echo $row[&#39;subcat&#39;]; ?&gt;&quot;&gt;
&lt;label for=&quot;sub_cat&quot;&gt; &lt;?php echo $row[&#39;subcat&#39;];  ?&gt;&lt;/label&gt;&lt;br&gt;

&lt;?php 
}


?&gt;

I am trying to solve with j query code attached but when I checked checkbox it displayed the subcategories of all checked checkboxes but when un check any of the box this will un display all the subcategories

答案1

得分: 0

问题在于,当取消选中任何复选框时,您会清除所有内容:

function showUser(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  }
  ...
}
$('input[type=checkbox]').on('change', function() {
    if($(this).is(':checked')) {
        showUser($(this).val());
    }
    else{
        showUser("");
    }

要解决此问题,您需要以某种方式识别具有给定 cat_id 的元素,例如,您可以添加数据属性:

<input type="checkbox" id="sub_cat" name="sub_cat" value="<?php echo $row['subcat']; ?>" data-cat="<?php echo $q ?>">
<label for="sub_cat" data-cat="<?php echo $q ?>"> <?php echo $row['subcat']; ?></label><br>

当您想取消选中复选框时,您可以使用该数据属性查找要删除的元素:

function hideUser(str) {
   $('[data-cat="' + str + '"]').remove();
}

$('input[type=checkbox]').on('change', function() {
    const user = $(this).val();
    if($(this).is(':checked')) {
        showUser(user);
    } else{
        hideUser(user);
    }
});
英文:

The problem is that you literally clear everything when uncheck any checkbox:

function showUser(str) {
  if (str == &quot;&quot;) {
    document.getElementById(&quot;txtHint&quot;).innerHTML = &quot;&quot;;
    return;
  }
  ...
}
$(&#39;input[type=checkbox]&#39;).on(&#39;change&#39;, function() {
    if($(this).is(&#39;:checked&#39;)) {
        showUser($(this).val());
    }
    
    
    else{
        
        showUser(&quot;&quot;);
    }

To solve the issue you need to identify somehow the elements that have given cat_id, e.g. you can add data attribute:

&lt;input type=&quot;checkbox&quot; id=&quot;sub_cat&quot; name=&quot;sub_cat&quot; value=&quot;&lt;?php echo $row[&#39;subcat&#39;]; ?&gt;&quot; data-cat=&quot;&lt;?php echo $q ?&gt;&quot;&gt;
&lt;label for=&quot;sub_cat&quot; data-cat=&quot;&lt;?php echo $q ?&gt;&quot;&gt; &lt;?php echo $row[&#39;subcat&#39;];  ?&gt;&lt;/label&gt;&lt;br&gt;

and when you want to uncheck the checkbox you can find elements you want to remove using that data attribute:

function hideUser(str) {
   $(&#39;[data-cat=&quot;&#39; + str + &#39;&quot;]&#39;).remove();
}

$(&#39;input[type=checkbox]&#39;).on(&#39;change&#39;, function() {
    const user = $(this).val();
    if($(this).is(&#39;:checked&#39;)) {
        showUser(user);
    } else{
        hideUser(user);
    }
});

huangapple
  • 本文由 发表于 2023年6月19日 05:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定