![](/img/trans.png)
[英]Why is my PHP echo returning a 0 in some places (yet working in others)?
[英]Why is AJAX only replacing my variable in some places?
我有以下AJAX代码,它用MYSQL数据库的“百分比”列中的数据替换了类“ percentreplacer”中的所有内容:
<script type='text/javascript'>
$(document).ready(function () {
$('#functionsquestionform2').on('submit', function(e) {
e.preventDefault();
$.ajax({
url : "aplaygroundajaxtest.php",
type: "POST",
data: $(this).serialize(),
success: function (data) {
$(".percentreplace").text(data.percent);
},
});
});
});
</script>
在脚本的另一部分中,我具有以下PHP代码段:
<?php echo '<span class="percentreplace">'.$data['FunctionsPercent'].'</span>'; ?>
当我运行代码时,顶部的AJAX代码成功地用存储在数据库中的百分比值(例如“ 6”)替换了上面的范围。
稍后在我的代码中,我尝试使用下面显示的JQuery脚本将此百分比设置为变量:
<script type='text/javascript'>
$(function(){
var carouselbutton1percentage='<?php echo '<span class="percentreplace">'.$data['FunctionsPercent'].'</span>'; ?>' ....[cont'd]
但是,在这里,不是用百分比代替整个PHP代码段(比如说6),而是将变量carouselbutton1percentage
设置为等于<span class="percentreplace">6</span>
他们在前者中做过。 我猜这与报价有关,但是我已经玩了很多次,我不知道该更改什么。
任何帮助将不胜感激!
谢谢
我可能会缺少一些东西。 但是,我认为您不需要像在AJAX响应块中那样真正地更新该html元素,而不是存储一个字符串“其中包含类似于PHP和jquery的字符”。 所以..
$( “percentreplace。”)文本( '6')。
要么
var carouselbutton1percentage = 6;
$( “percentreplace。”)文本(carouselbutton1percentage)。
但是再一次,也许我误会了。
我认为您需要适当地转义字符串吗?
var carouselbutton1percentage='<?php echo \'<span class="percentreplace">\'.$data[\'FunctionsPercent\'].\'</span>\'; ?>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.