How to print ajax response to span class

时间:2017-11-13 06:10:32

标签: javascript php jquery ajax

I wanted to show ajax response to following HTML where initially 0 available slot which could be changed on ajax response. and based on data response dropdown_wrapper class < li> will come automatically. How do i make it dynamic?

<span class="selected_item">0 available Slots</span>
<div class="form_item_dropdown">
    <div class="dropdown_wrapper">
        <li class="timeSlot" id="time1">
            <i class="icon fa fa-clock-o" aria-hidden="true">
            </i> 
            <span class="item_title">10am - 4pm (value comes through Ajax)</span>
        </li>
    </div>
</div>

Ajax code :

   $(".school_title").on("click", function (argument) {
        var id = $(this).attr('id');      
        var info=$.get("{{url('school-slot')}}",{id:id});
        info.done(function(data){               
            console.log(data.length);  
  /*        $.each(data,function(index,subcatObj){      
            });  */
        });
    });

Here is the response :

{id: 3, school_id: 3, break_time: "10am - 8pm"}

{id: 4, school_id: 3, break_time: "10am - 4:30pm"}

3 个答案:

答案 0 :(得分:5)

Just change the content of foreach loop.

var divContent = '';
$.each(data,function(index,subcatObj){
    divContent += '<li class="timeSlot" id="time'+subcatObj.id+'"><i class="icon fa fa-clock-o" aria-hidden="true"></i><span class="item_title">'+subcatObj.break_time+'</span></li>';
});
$(".dropdown_wrapper").append(divContent);

答案 1 :(得分:0)

change ajax code Like this . use javascrpt string concatenate +=

 `$(".school_title").on("click", function (argument) {
 var id = $(this).attr('id');      
 var info=$.get("{{url('school-slot')}}",{id:id});
 info.done(function(data){  
 data= JSON.parse(data);
 console.log(data.length);  
 html="";
 $.each(data,function(index,subcatObj){ 
 html +="<li class="timeSlot" id="time'+subcatObj+'"><i class="icon fa fa-
 clock-o" aria-hidden="true"></i> ";
 });
 $(".dropdown_wrapper").append(html);
 });
 });

`

答案 2 :(得分:0)

If i understand your problem. May be You try this:-

<span class="selected_item">0 available Slots</span>
<div class="form_item_dropdown">
<div class="dropdown_wrapper">
    <li class="timeSlot" id="time1">
        <i class="icon fa fa-clock-o" aria-hidden="true">
        </i> 
        <span id="GiveDyanmicIdhere3" class="item_title">10am - 4pm (value comes through Ajax)</span> Give dyanmic id here
    </li>
  </div>
</div>
<div class="form_item_dropdown">
<div class="dropdown_wrapper">
    <li class="timeSlot" id="time1">
        <i class="icon fa fa-clock-o" aria-hidden="true">
        </i> 
        <span id="GiveDyanmicIdhere4" class="item_title">10am - 4pm (value comes through Ajax)</span>  // Give dyanmic id here
    </li>
</div>

 var data = [ 
    {id: 3, school_id: 3, break_time: "10am - 8pm"}

    {id: 4, school_id: 3, break_time: "10am - 4:30pm"}
 ]; // save respone in one variable

 $.each(data, function(i, item) {
   $("#GiveDyanmicIdhere"+data[i].id).append(data[i].break_time);
                  Or
    $("#GiveDyanmicIdhere"+data[i].id).text(data[i].break_time);  
 });​

You can try append, text, html according to your requirement