我有一個按鈕,使ajax調用一個php文件,該文件輪流發送一些html數據作爲響應。通過ajax響應元素循環
$.ajax({
type: 'POST',
data: {
cherry: cherry,
chocolatechip: chocolatechip,
butterscotchchip: butterscotch,
gems: gems,
sweetner: sweetner
},
url: 'customcakebox.php',
success: function (content) {
}
});
HTML響應包含這三個要素:
<img id="abc1" src="ski.png" height="13px" width="15px">
<img id="abc2" src="cho.png" height="15px" width="15px">
<img id="abc3" src="cho.png" height="15px" width="15px">
然而,這些元件可以減少或取決於在php文件中指定的條件的增加。
我想做的是循環這些元素,只打印那些我想打印的元素?
注:我知道你會說,爲什麼我就不能過,我需要證明。好了的,問題是我想隨機顯示DIV中的每一個元素,並以使這項工作我確實需要每一個。
Random positon of elements inside div這就是我想實現(這是僅供參考,以我所試圖做的)
PHP文件:
require 'connect.inc.php';
require 'session/inc.encrypt.php';
$cherry = $_REQUEST['cherry'];
$chocolatechip = $_REQUEST['chocolatechip'];
$butterscotchchip = $_REQUEST['butterscotchchip'];
$gems = $_REQUEST['gems'];
$sweetner=$_REQUEST['sweetner'];
$items = '';
if ($cherry > 20)
$cherry = 20;
else if ($cherry < 0)
$cherry = 0;
if ($chocolatechip > 20)
$chocolatechip = 20;
else if ($chocolatechip < 0)
$chocolatechip = 0;
if ($butterscotchchip > 20)
$butterscotchchip = 20;
else if ($butterscotchchip < 0)
$butterscotchchip = 0;
if ($gems > 20)
$gems = 20;
else if ($gems < 0)
$gems = 0;
if ((!empty($cherry) || $cherry == 0) && (!empty($chocolatechip) || $chocolatechip == 0) && (!empty($butterscotchchip) || $butterscotchchip == 0) && (!empty($gems) || $gems == 0)) {
for ($i = 0; $i < $cherry; $i++)
{
$items .= ' <img src="ski.png" height="13px" width="15px">';
}
for ($i = 0; $i < $chocolatechip; $i++)
$items .= ' <img src="cho.png" height="15px" width="15px">';
for ($i = 0; $i < $butterscotchchip; $i++)
$items .= '<img src="che.png" height="15px" width="15px">';
for ($i = 0; $i < $gems; $i++)
$items .= '<img src="but.png" height="15px" width="15px">';
}
$customcake['cherry']=$cherry;
$customcake['chocolatechip']=$chocolatechip;
$customcake['butterscotchchip']=$butterscotchchip;
$customcake['gems']=$gems;
$customcake['sweetner']=$sweetner;
$_SESSION['customcake']=$customcake;
echo $items;
IDS一直沒有請在這裏設置。請不要提及,我會在稍後設置。
你可以改變.php返回一個包含圖像標記的JSON對象嗎?這樣你將有一個立即可枚舉的對象。JSON的應該是這樣的: { 「IMG1」: 「
」, 「IMG2」: 「
」, 「IMG3」: 「
」 } –
@ScottMarcus郵政與解釋關於此評論 –
你一個答案可以將響應加載到臨時元素中作爲子元素,然後查詢臨時對象的子節點,然後將其放入數組中,分配隨機順序,然後將它們重新寫回永久元素。 –