1

我遇到了 php javaScript echo 的问题。我想为使用 echo 动态创建的按钮触发一个 javaScript 函数。

当我在没有回声的情况下对其进行测试时,功能正在正常使用。但是当我使用 echo 创建它时,它不起作用。

这是 pages.php 中的回声:

$cont.='<div style="border-bottom: 1px solid #A2DBFF; padding-bottom: 25px; padding-top: 20px; margin-bottom: 10px; margin-left: 25px;">';
$cont.='<div class="SUBH">
        <input type="text" id="txtNEWHEADING" name="txtSubNam" placeholder="The new heading"  />
    </div>';
$cont.='<div class="PTAG">
        <p>
            <textarea rows="5" id="disNEWCONT" cols="70" placeholder="add a discription here.." name="txtCont"></textarea>
        </p>
    </div>';
$cont.='<div class="IMGTAG">
        <input type="file" id="imgNewIMAGE" />
        <button class="btnSav"  type="submit" onclick="SaveContent()" id="btnNEWCONTENT" style="margin-left: 300px;" />save</button>
    </div>';
$cont .= '</div>';

JavaScript 在这里:

$cont .= '
    <script type="text/javascript">

        $("#btnNEWCONTENT").click(function(){
            alert("hi there..");
            $.ajax({ url: \'ajax.php\',
                data: {action: \'test\'},
                type: \'post\',
                success: function(data) {
                    alert(data);
                }
            });
        });     
    </script>
    ';
echo $cont;

请帮忙。

4

1 回答 1

2

这段代码对我有用,在 head 之间添加了 jquery,并将 javascript 包装到函数中:

<html>
<head>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
</head>
<body>
<?php

 $cont='<div style="border-bottom: 1px solid #A2DBFF; padding-bottom: 25px; padding-top: 20px; margin-bottom: 10px; margin-left: 25px;">';
    $cont.='<div class="SUBH">
                <input type="text" id="txtNEWHEADING" name="txtSubNam" placeholder="The new heading"  />
            </div>';
    $cont.='<div class="PTAG">
               <p>
                  <textarea rows="5" id="disNEWCONT" cols="70" placeholder="add a discription here.." name="txtCont"></textarea>
              </p>
            </div>';
    $cont.='<div class="IMGTAG">
              <input type="file" id="imgNewIMAGE" />
              <button class="btnSav"  type="submit" onclick="SaveContent()" id="btnNEWCONTENT" style="margin-left: 300px;" />save</button>
            </div>';
$cont.='</div>';

$cont.='
    <script type="text/javascript">
    function SaveContent(){
        $("#btnNEWCONTENT").click(function(){

            alert("hi there..");
            $.ajax({ url: \'ajax.php\',
             data: {action: \'test\'},
             type: \'post\',
             success: function(data) {
                 alert(data);
             }
        });

    });    } 
    </script>
    ';
    echo $cont;
?>
</body>
</html>
于 2013-09-29T04:01:13.333 回答