簡體   English   中英

為什么在某些地方AJAX只替換我的變量?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM