循环遍历 AJAX 响应元素
loop through ajax response elements
我有一个按钮,它对一个php文件进行ajax调用,而php文件又发送一些html数据作为响应。
$.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中随机显示每个元素并使其工作。我确实需要他们中的每一个。
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;
此处尚未设置 ID。请不要提及.我稍后会设置它。
假设您返回的响应位于一个名为 content 的变量中,然后将此代码置于运行循环中
$(document).ready(function() {
$(content).filter('img').each(function(index,item) {
alert(item);
});
});
如果你的响应返回的不仅仅是图像对象,那么你可以使用$(content).filter('img')。
如果你的.php可以用 JSON 响应,如下所示:
{
"img1": "<img id='abc1' src='ski.png' height='13px' width='15px'>",
"img2": "<img id='abc2' src='cho.png' height='15px' width='15px'>",
"img3": "<img id='abc3' src='cho.png' height='15px' width='15px'>"
}
当您收到结果(作为成功回调的content
参数)时,您可以像这样循环访问响应:
var jContent = JSON.parse(content);
for(var i = 0; i < jContent.length; ++i){
// whatever you need to do
console.log(jContent.img1); // <img id='abc1' src='ski.png' height='13px' width='15px'>
}
您需要在 PHP 文件中创建一个元素数组,然后将其作为 JSON 对象返回。
json_encode( $img_array, JSON_UNESCAPED_UNICODE );
然后,您可以使用成功函数中的该数组来制作您的东西。不要忘记传递选项数据类型。
$.ajax({
type: 'POST',
data: {
cherry: cherry,
chocolatechip: chocolatechip,
butterscotchchip: butterscotch,
gems: gems,
sweetner: sweetner
},
url: 'customcakebox.php',
success: function (content) {
},
dataType : "json"
});
相关文章:
- 通过iron-ajax初始化对象数组(链接到caller's元素的响应)
- 响应画布元素
- 为什么不是't html<对象>元素响应鼠标事件
- 如何在jquery ajax成功html响应中找到输入隐藏元素
- 按钮元素没有响应
- 单击按钮后的无响应元素
- 自动完成搜索无法正常工作,请不要迭代$中的所有元素.每个ul-li都会及时显示响应
- 原型js 如何使定期更新程序隐藏基于ajax响应的类元素
- Jquery-(ajax async:true)元素大小没有响应
- 为什么jquery元素在ipad上没有响应
- 页面元素停止响应
- 如何从html响应解析javascript数组(包含JSON对象作为元素)
- 循环遍历 AJAX 响应元素
- 多个元素响应移动Webkit和Hammer JS上的触摸事件
- 将元素添加到 Ajax 响应返回的表单中
- 如何使图像DOM元素响应
- 我怎么能强迫元素在一个固定的定位元素响应javascript的onclick事件在Android web浏览器
- 如何使用CSS @media查询使这个元素响应
- 基于部分元素响应解析JSON数据
- JS窗口宽度html5视频元素响应