简体   繁体   English

如何使用jQuery访问动态ID标签

[英]How to access dynamic id tag with jquery

I'm trying to update the paragraph tag in foreach with jquery selector in my second ajax call. 我试图在第二个ajax调用中使用jQuery选择器更新foreach中的段落标签。 I made the id tag unique by setting it to id="spots_,+item.id " but don't know how to access the dynamic id tag outside the foreach loop. 我通过将id标记设置为id="spots_,+item.id ”来使其唯一,但是不知道如何在foreach循环之外访问动态id标记。 I keep getting "id is not defined" error. 我不断收到“未定义ID”错误。 Thought maybe a global variable would work but no success. 认为也许全局变量会起作用,但不会成功。

//ajax form the get available times to play
$('#form').submit(function(){
$.ajax({
  url: $('#form').attr('action'),
  type: 'POST',
  data : $('#form').serialize(),
     success: function(response){
      $.each(JSON.parse(response), function(i, item) {
          var jdate = $('#date').val();

        $('<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">' +  '</form>' + "</td><td>" + "Spots:" + '<p class="spots" id="spots_' + id + '"'+ ">" + '<div id="spots"></div>' + '</p>'  +  "</td>").appendTo('#availableTimes');
        });//end loop                   

    //ajax form the get available spots/seats 
$('.insideForm').submit(function(){
var form = $(this).closest('form');

$.ajax({
  url: $(this).attr('action'),
  type: 'POST',
  data : $(this).serialize(),
          success: function(response){        
        $('#spots_'+id).html(response);         
      }//end success      
    });
   return false;
});                    
  }//end success
});
return false;
});//end ajax time form

In your .insideForm object you only have one .spots classed paragraph. 在.insideForm对象中,只有一个.spots分类段落。

Try using the jQuery selector inside the form: 尝试在表单内使用jQuery选择器:

$('.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

    });
    return false;
});

In your $.ajax call change url: $(this).attr('action') to url: form.attr('action') . $.ajax调用中,将url: $(this).attr('action')更改为url: form.attr('action') Inside the callback, $(this) refers to the jqXHR object of the ajax call, not the element the event handler was bound to. 在回调内部, $(this)引用ajax调用的jqXHR对象,而不是事件处理程序绑定到的元素。

EDIT 编辑

I also changed $(this).serialize() to form.serialize() for the same reason above. 我也出于上述相同原因将$(this).serialize()更改为form.serialize()

//ajax form the get available spots/seats 
$('.insideForm').submit(function() {
  var form = $(this).closest('form');

  $.ajax({
    url: form.attr('action'),
    type: 'POST',
    data: form.serialize(),
    success: function(response) {
      $('#spots_' + id).html(response);
    } //end success      
  });
  return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM