dongtan1009 2016-07-18 10:04
浏览 106

附加在嵌套在jquery.each中的$ .post内部

PLEASE NOTE: I'm not getting any errors, just not functioning the way my logic says it should.

I'm attempting to create a calendar application, essentially my goal at this stage, is on the $document.load of the calendar page, I wish for it to loop through each day of the calendar (1 month) and for each day, send a request to PHP (AJAX, $.post()), query database, and then append those results onto the element it is currently accessing with the.each() function.

What I want/expect to happen:

I expect the results that are passed back from PHP to be appended onto the current day element it is posting off for.

What does happen:

Unfortunately what seems to be happening, is that the post is done for each element (day in the month) but the append, is only happening once and appending to the last element in the set, although as you can see below, my.append is inside the.each()

Please help guys, I'm not sure how I can make it pause after each post and append to the current element, instead of the last!

CODE LISTING:

HTML:

<ul class="days">
  <li><span>1</span></li>
  <li><span>2</span></li>
  <li><span>3</span></li>
  <li><span>4</span></li>
  <li><span>5</span></li>
  <li><span>6</span></li>
  <li><span>7</span></li>
  <li><span>8</span></li>
  <li><span>9</span></li>
  <li><span class="active">10</span></li>
  <li><span>11</span></li>
  <li><span>12</span></li>
  <li><span>13</span></li>
  <li><span>14</span></li>
  <li><span>15</span></li>
  <li><span>16</span></li>
  <li><span>17</span></li>
  <li><span>18</span></li>
  <li><span>19</span></li>
  <li><span>20</span></li>
  <li><span>21</span></li>
  <li><span>22</span></li>
  <li><span>23</span></li>
  <li><span>24</span></li>
  <li><span>25</span></li>
  <li><span>26</span></li>
  <li><span>27</span></li>
  <li><span>28</span></li>
  <li><span>29</span></li>
  <li><span>30</span></li>
  <li><span>31</span></li>
</ul>

jQuery:

$(".days li").each(function()
    {
        $dayNumber = $(this).text();
        $this = $(this);

        $.post("getShiftsMV.php", {dayNumber:$dayNumber}, function(results)
        {

               $this.append(results);

        });

    });

PHP:

$shifts = 0;

if($_POST["dayNumber"] != "" )
{
    $day = $_POST['dayNumber'];

     $sql = "SELECT * FROM shifts WHERE shift_date = '$day'";

     $run = $con->query($sql);

     if($run->num_rows > 0)
     {
         while($row = $run->fetch_assoc())
         {
             $shifts++;
         }
     }

    if($shifts > 0)
    {

       echo'<span class="label label-success">' . $shifts .'</span>';
    }
    else
    {
        echo'';
    }

}

=========================================================================

EDIT:

Using $var = "test"; for example, does work, as it will run in the console just fine.

$var being used without error

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输