JavaScript不适用于php echo [英] JavaScript doesn't work with php echo
本文介绍了JavaScript不适用于php echo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在php javaScript echo中遇到问题.我想为通过使用echo动态创建的按钮触发javaScript函数.
I'm having a problem with php javaScript echo. I wanna fire a javaScript functions for buttons which are dynamically created by using echo.
当我在没有回声的情况下对其进行测试时,功能正常使用.但是当我使用echo创建它时,它不起作用.
When i test it without echo, functions are using properly. But when I create it using echo, it doesn't work.
这是pages.php中的回显:
Here is the echo in 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在这里:
And the javaScript is here:
$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;
请帮助.
推荐答案
此代码对我有用,在head之间添加了jquery,并将javascript包装为函数:
This code is working for me ,added jquery between head , and wraped javascript into function:
<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>
这篇关于JavaScript不适用于php echo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文