PHP 到 JQuery 代码转换 [If else]

PHP to JQuery code conversion [If else]

如何在 JQuery 上完成?当我点击 hide 时,该元素将被隐藏,当我点击 show 时,该元素将被显示。我要怎么做?谢谢!

<form method="POST">

<button type="submit" name="hidden" >Hide</button>
<button type="submit" name="submit" >Show</button>

</form>

<?php 

    if (isset($_POST['hidden']) == true){
        echo '*hidden*';
    } else if ($_POST['']) {
        echo 'shown';
    }

?>  

你可以这样使用js,

document.getElementById('domid').style.visibility='hidden';

document.getElementById('domid').style.visibility='visible';
$(document).ready(function(){
   $(input([name='hidden']).click(function(){
      $(this).hide();
   });
    $(input([name='submit']).click(function(){
      $(input([name='hidden']).show();
   });
});

我知道你要求 JQuery,最好的选择是直接 JS,如此处另一个 post 所述。但是使用 php,您可以定义一些 classes,然后更改您希望在正确提交 $_POST 值时控制显示的标签中的 class 值。

<?php 

$result = ''; //You could place your default value here or run it through an if/else or switch stmt
if(isset($_POST['show'])){
        $result = 'show';
    } else if ($_POST['hidden']) {
        $result = 'hide';
    } else {
        $result = 'default';
    }

?>

<style>
    .show {
        display:block;
    }
    .hide {
       display:none;
    }
    .default {
       //however you wish your default CSS to act hidden or visible
    }
</style>
<body>
<form method="POST">

<button type="submit" name="hidden" >Hide</button>
<button type="submit" name="show" >Show</button>

</form>

<div id="theElement" class="<?=$result?>"></div>
</body> 

您在 jQuery 上的等效代码是下一个:

$(() => {
    $("#hide-but").on("click", () => $("#result").html("*hidden*"));
    $("#show-but").on("click", () => $("#result").html("shown"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="button-group">
  <button type="submit" name="hidden" id="hide-but">Hide</button>
  <button type="submit" name="submit" id="show-but">Show</button>
</div>

<div id="result">shown</div>