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条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站