简体   繁体   中英

How to hide country parameters on page load via jquery ajax php

How to hide country parameters on page load via jquery ajax php.

The code below display results from database and its working fine.

I can hide/unhide or toggle all the country values each separately via its id when button is click and is working good.

Here is my issue: How can I hide only the all country values on page load so that I can toggle or hide/unhide it as usual.

here is my coding effort so far屏幕截图显示指向我想在页面加载时隐藏的国家/地区值的箭头

<html>
    <head>
  <script src="jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
       <script>
//toggle div on click

       $(document).ready(function() {

            $(".hideunhide_country").click(function(){
             var id = this.id; 
               $("#result_"+id).toggle( 'slow', function(){

               });
            });
         });

/*

//hide unhide div on click
$(document).ready(function(){
    $(".hideunhide_country").click(function(){
var id = this.id; 
$("#result_"+id).hide();

  });
});
*/

</script>



        <div class="content">

            <?php

include('db.php');
$result = $db->prepare('SELECT * FROM users order by id');
$result->execute();
while ($row = $result->fetch()) {


$id = $row['id'];
$country = $row['country'];



            ?>

                    <div class="p">
 <h1>Userid: <?php echo $id; ?></h1>


//hide country values on page load
<div id="result_<?php echo $id; ?>">
 <h1><?php echo $country; ?></h1>
</div>

<input type="button" value="HideUnhide" id="<?php echo $id; ?>" class="hideunhide_country"  />

</div>


            <?php

                }
            ?>



        </div>
    </body>
</html>

Only a qucik fix, you schould refactor your code...

<html>
    <head>
  <script src="jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
       <script>
//toggle div on click

       $(document).ready(function() {

            $(".hideunhide_country").click(function(){
             var id = this.id; 
               $("#result_"+id).toggle( 'slow', function(){

               });
            });
            // hide all countries
            $("[id^='result_']").hide()
         });

/*

//hide unhide div on click
$(document).ready(function(){
    $(".hideunhide_country").click(function(){
var id = this.id; 
$("#result_"+id).hide();

  });
});
*/

</script>



        <div class="content">

            <?php

include('db.php');
$result = $db->prepare('SELECT * FROM users order by id');
$result->execute();
while ($row = $result->fetch()) {


$id = $row['id'];
$country = $row['country'];



            ?>

                    <div class="p">
 <h1>Userid: <?php echo $id; ?></h1>


//hide country values on page load
<div id="result_<?php echo $id; ?>">
 <h1><?php echo $country; ?></h1>
</div>

<input type="button" value="HideUnhide" id="<?php echo $id; ?>" class="hideunhide_country"  />

</div>


            <?php

                }
            ?>



        </div>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM