亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我使用 php 變量時,JQuery 無法加載

當我使用 php 變量時,JQuery 無法加載

月關寶盒 2023-07-06 14:37:03
我將 PHP 代碼合并到 JQuery 中,如下所示:                echo "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>                <script type='text/javascript'>                $(document).ready(function() {                    var count = ($('.data').length + 25);                    /* This is the line */var c = $c;                    $('#data').load('data.php?v=' + count + '&c=' + c)                    $('.btn-data').click(function() {                        count = count + 25;                        $('#data').load('data.php?v=' + count + '&c=' + c)                    })                })</script>";PHP 變量顯示為藍色,沒有顯示錯誤,但當我運行它時,它拒絕工作。我已經測試了其他所有內容,并且 100% 確定 PHP 是導致問題的原因。我怎樣才能解決這個問題?
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

如果$c是你的 php 變量,你需要引用它并將其加入echo標簽內。


如果它是一個integer,這應該可以...

<?php


echo "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>

? ? ? <script type='text/javascript'>

? ? ? ? ? $(document).ready(function() {

? ? ? ? ? var count = ($('.data').length + 25);

? ? ? ? ? var c = " . $c . ";

? ? ? ? ? $('#data').load('data.php')

? ? ? ? ? ? ?$('.btn-data').click(function() {

? ? ? ? ? ? ? ? ?count = count + 25;

? ? ? ? ? ? ? ? ? $('#data').load('data.php?v=' + count + '&c=' + c)

? ? ? ? ? ? ?})

? ? ? ? ? });

? ? ? </script>";


如果這是一個,string這應該可以工作......


<?php


echo "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>

? ? ? <script type='text/javascript'>

? ? ? ? ? $(document).ready(function() {

? ? ? ? ? var count = ($('.data').length + 25);

? ? ? ? ? var c = '" . $c . "';

? ? ? ? ? $('#data').load('data.php')

? ? ? ? ? ? ?$('.btn-data').click(function() {

? ? ? ? ? ? ? ? ?count = count + 25;

? ? ? ? ? ? ? ? ? $('#data').load('data.php?v=' + count + '&c=' + c)

? ? ? ? ? ? ?})

? ? ? ? ? });

? ? ? </script>";


如果它是一個object,您將必須解析 var 或對其進行編碼。




有時,如果您想在 php 中回顯或返回大塊 html,可以使用輸出緩沖區來捕獲 html,然后您可以通過函數返回 html,或回顯它。當你的 html 沒有包含在 echo 引號標簽內時,這只會讓你的 html 更容易閱讀。


<?php


// c sting php var?

$c = 'string';


// start output buffer

ob_start();


?>


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">

? ? $(document).ready(function() {

? ? ? ? let count = ($('.data').length + 25);

? ? ? ? let c = '<?=$c?>';

? ? ? ? $('#data').load('data.php');

? ? ? ? $(this).on('click', '.btn-data', function() {

? ? ? ? ? ? count = count + 25;

? ? ? ? ? ? $('#data').load('data.php?v=' + count + '&c=' + c);

? ? ? ? });

? ? });

</script>


<?php


// store everything between ob_start() and here into a php variable

$script = ob_get_clean();


// return script html

return $script;


// or echo script html

echo $script;


// not both together obviously


?>


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號