如何在foreach语句中访问选择器?

时间:2018-02-11 23:03:06

标签: javascript jquery html ajax

当用户点击jquery datepicker上的某个日期时,我会触发两个ajax调用。

在第二个ajax调用中,我希望它在第一个ajax调用中将响应/数据填充到段落tag class="spots",以显示该特定时间的可用点。

我的问题是第二个ajax调用只填充&am​​p;重复它找到的第一个结果。我认为这是因为我的类选择器在foreach行中是相同的。我已设置id + item.id以使其动态化,但如何在jquery选择器中访问它们?任何帮助,将不胜感激。请参阅下面的代码。

$(document).ready(function() {

//show datpicker calendar set getDay function on select
$( "#datepicker" ).datepicker({
    numberOfMonths: 1,
    showButtonPanel: true,
    onSelect: getDay,

});

 function getDay() {

 var date1 = $('#datepicker').datepicker('getDate');
 var day = date1.getDay();

//set hidden input to numberical value of day
    $('#dayOfWeek').val(day);   
//set hidden textbox value near datepicker to submit date in proper format for db
    $('#date').val($.datepicker.formatDate('yy-mm-dd', date1));  



//ajax form the get available times to play
$.ajax({
  url: $('#form').attr('action'),
  type: 'POST',
  data : $('#form').serialize(),
     success: function(response){

     //clear results before showing another date selected
      $('.table').html("");

      //loop through json results and build table
      $.each(JSON.parse(response), function(i, item) {
          var jdate = $('#date').val();
          var id = item.id;


        $('<tr>').html("<td>" + item.time + "</td><td>"  + '<input type="text" name="jtime" value="' + item.time + '"' + "/>"  + '<input type="text" name="jdate" value="' + jdate + '"' + ">"  + "Spots:" + '<p class="spots" id="spots_' + id + '"'+ ">" + '</p>'  + "</td>").appendTo('#availableTimes');

        });//end loop 

            //fire getSpots function
            getSpots();     

  }//end success
});
return false;

};   //end getDay function

//  get available spots
function getSpots(){

var values = {
        'jtime': $('input[name="jtime"]').val(),
        'jdate': $('input[name="jdate"]').val(),
};

$.ajax({
  //url: form.attr('action'),
 url: '/reservations/getSpots',
  type: 'POST',
 // data : form.serialize(),
 data : values,
      success: function(response){

        $('.spots').html(response);

      }//end success


    }); //end ajax
   return false;

};//end getSpots function


    })//end doc ready            



             </script>

以下是一段有效的代码,但它使用带有按钮的表单来提交第二个ajax调用。如果没有按钮提交,我希望它能像这样工作。想要在选择数据标签日期时发布第二个ajax调用。也许我正在考虑这个错误。

//show datpicker calendar set getDay function on select
$( "#datepicker" ).datepicker({
    numberOfMonths: 1,
    showButtonPanel: true,
    onSelect: getDay
});


})//end doc ready
 function getDay() {

 var date1 = $('#datepicker').datepicker('getDate');
 var day = date1.getDay();

//set hidden input to numberical value of day
    $('#dayOfWeek').val(day);   
//set hidden textbox value near datepicker to submit date in proper format for db
    $('#date').val($.datepicker.formatDate('yy-mm-dd', date1));  



//ajax form the get available times to play
$.ajax({
  url: $('#form').attr('action'),
  type: 'POST',
  data : $('#form').serialize(),
     success: function(response){

     //clear results before showing another date selected
      $('.table').html("");

      //loop through json results and build table
      $.each(JSON.parse(response), function(i, item) {
          var jdate = $('#date').val();
          var id = item.id;


        $('<tr>').html("<td>" + item.time + "</td><td>" + '<form  class="insideForm" action="/reservations/getSpots" accept-charset="utf-8"  method="">'  + '<input type="text" name="jtime" value="' + item.time + '"' + "/>"  + '<input type="text" name="jdate" value="' + jdate + '"' + ">" + '<input type="submit" class="btn btn-primary" value="Spots">' + "Spots:" + '<p class="spots" id="spots_' + id + '"'+ ">" + '</p>' + '</form>' + "</td>").appendTo('#availableTimes');

        });//end loop 


        getSpots();


  }//end success
});
  return false;


};   //end getDay function



//  get available spots
function getSpots(){
            //ajax form the get available spots 


$('.insideForm').submit(function(){
var form = $(this).closest('form');

$.ajax({
  url: form.attr('action'),
  type: 'POST',
  data : form.serialize(),

      success: function(response){

        $('.spots', form).html(response);

      }//end success


    }); //end ajax
 return false;
}); //end submit 

};//end getSpots function

1 个答案:

答案 0 :(得分:1)

您调用getSpots一次并期望所有内容都更新?你需要做的是为每一行调用getSpots一次,将id传递给getSpots,这样getSpots可以使用正确的输入更新正确的行

请参阅标记为// ****的行,以了解代码的更改

  

删除的答案有更好的方法

function getDay() {
    var date1 = $('#datepicker').datepicker('getDate');
    var day = date1.getDay();
    //set hidden input to numberical value of day
    $('#dayOfWeek').val(day);
    //set hidden textbox value near datepicker to submit date in proper format for db
    $('#date').val($.datepicker.formatDate('yy-mm-dd', date1));
    //ajax form the get available times to play
    $.ajax({
        url: $('#form').attr('action'),
        type: 'POST',
        data: $('#form').serialize(),
        success: function (response) {
            //clear results before showing another date selected
            $('.table').html('');
            //loop through json results and build table
            $.each(JSON.parse(response), function (i, item) {
                var jdate = $('#date').val();
                var id = item.id;
                $('<tr>').html('<td>' + item.time + '</td><td>' + '<input type="text" name="jtime" value="' + item.time + '"' + '/>' + '<input type="text" name="jdate" value="' + jdate + '"' + '>' + 'Spots:' + '<p class="spots" id="spots_' + id + '"' + '>' + '</p>' + '</td>').appendTo('#availableTimes');
                // **** call getSpots for every row
                getSpots('#spots_'+id, item.time, jdate);
            }); //end loop 
        } //end success

    });
    return false;
}    //end getDay function
//  get available spots
// **** accept output id, jtime and jdate
function getSpots(id, jtime, jdate) {
    // get the inputs for the current id
    var values = {
        'jtime': jtime,
        'jdate': jdate,
    };
    $.ajax({
        //url: form.attr('action'),
        url: '/reservations/getSpots',
        type: 'POST',
        // data : form.serialize(),
        data: values,
        success: function (response) {
            // **** update the spots for current id
            $(id).html(response);
        } //end success

    }); //end ajax
    return false;
} //end getSpots function