2013-09-29 59 views
0

我遇到了一个php javaScript回显问题。我想为通过使用echo动态创建的按钮启动一个javaScript函数。JavaScript不适用于php echo

当我测试它没有回声,功能正常使用。但是当我使用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; 

请帮助。

+0

什么时候jQuery库负荷? – Teddy

+0

没有理由说2个相同的HTML页面的工作方式不同,你能发布这2个页面吗? –

+0

PHP只是生成发送给浏览器的输出。在浏览器中查看源代码以查看它输出的内容,并查看是否可以发现它不是您想要的内容。 – IMSoP

回答

2

此代码为我工作,增加头部和包梁JavaScript之间的jQuery的功能到:

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