duanbage2161 2017-04-04 11:26
浏览 70
已采纳

如何使jQuery / Javascript在PHP语句中工作?

I'm having some difficulties getting Javascript to execute in a PHP statement if it is possible at all. Below I have the code, if the field 'First' is empty, then the background color of a certain div will be yellow. How can I get this to work properly if it is possible in the first place? Thanks in advance.

if (empty($_POST["First"])) {

    echo '<script type="text/javascript">';
    echo '$("div").css({"background-color":"yellow"});';
    echo '</script>';

} 
  • 写回答

2条回答 默认 最新

  • duangu9173 2017-04-04 11:38
    关注

    check this code. hope it will help you

    <div>Content Div</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <?php
     //$_POST["First"] = ''; if you want to check this code execute then remove comment then run this code
     if (empty($_POST["First"])) {
    echo '<script type="text/javascript">';
    echo '$("div").css("background-color","yellow");';
    echo '</script>';
    }
    ?>
    

    you can also use this code

    <div>Content Div</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <?php
    //$_POST["First"] = ''; this is for test case if you want to check then remove comment then run this code
     if (empty($_POST["First"])) { ?>
    <script type="text/javascript">
    $("div").css("background-color","yellow");
    </script>
    <?php } ?>
    

    in this code php tag and javascript tag use separate

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?