PHP JSON and <script> tag


PHP JSON and <script> tag

我使用PHP发送json字符串jQuery ajax。我只是使用$("#id").html(data.result)来显示从获取的页面的html。现在,在获取的页面上,我正在遍历MySQL结果。我需要在每个结果上运行一段javascript,这样我就可以为每个结果启动一个倒计时计时器。

我的问题是调用页面只是将实际的javascript脚本显示为字符串。

$row_array['result'] .= '<script>trigger script</script>'

有什么建议吗?

谢谢

如果data.result的一部分是javascript,则不执行。试试这个:

$script = 'alert("this is javascript");';
$html = '<b>blah blah this is html<b>';
$row_array['result'] = json_encode(array('script' => $script, 'html' => $html));

然后用

代替$("#id").html(data.result)
eval(data.result.script)
$("#id").html(data.result.html)