无法将一个javascript变量传递给一个php标记
问题描述:
我想在HTML页面的标记中显示一个javascript变量,但当我单击该按钮时它不会显示。谁能告诉我我做错了什么?无法将一个javascript变量传递给一个php标记
HTML
<button name = "presser">go </button>
<p id = "display"></p>
<script type ="text/javascript" src ="jquery.js"></script>
<script type ="text/javascript" src = "processor_file.js"></script>
的Javascript
$(":button").click(function(){
$.get("middleman.php", {theword: "happen"},function(data,status){
$.getElementById("display").innerHTML = data;
});
});
PHP代码middleman.php
<?php
if(isset($_GET["theword"])){
$token = $_GET["theword"];
echo substr($token, 1, 3);
}
?>
这个词是成功传递到JavaScript页面从PHP页面,但是当我尝试通过<p>
标签显示它,没有任何反应。
答
你混合香草JS和jQuery,你应该做到如下:
$(":button").click(function(){
$.get("middleman.php", {theword: "happen"},function(data,status){
$("#display").html(data);
});
});
+0
非常感谢,解决了我的问题 – Sparksido
答
$.getElementById
应该document.getElementById
。或者使用jQuery功能
$("#display").html(data);
Javascript控制台中的任何错误?像说'$ .getElementById'不是函数? – Barmar