简体   繁体   English

如何解决这个jquery / ajax问题?

[英]How I fix this jquery / ajax issue?

Basically I am having a table like below: 基本上我有一个如下表:

<tbody>   
  <tr>
    <td><img class="img-thumbnail" src="./images/no_image.png" width="100" height="100"></td>
    <td><button class="change_image" data-image_id="1">Change Image</button></td>
    <td><input type="text" name="sort[]" size="7" placeholder="Sort order"></td>      
  </tr>    
  <tr>
    <td><img class="img-thumbnail" src="./images/no_image.png" width="100" height="100"></td>
    <td><button class="change_image" data-image_id="2">Change Image</button></td>
    <td><input type="text" name="sort[]" size="7" placeholder="Sort order"></td>      
  </tr>    
  <tr>
    <td><img class="img-thumbnail" src="./images/no_image.png" width="100" height="100"></td>
    <td><button class="change_image" data-image_id="3">Change Image</button></td>
    <td><input type="text" name="sort[]" size="7" placeholder="Sort order"></td>      
  </tr>
</tbody>

When click on .change_image button, its opening a modal to change that image. 单击.change_image按钮时,它会打开一个模态以更改该图像。

This is how my modal open: 这是我的模态打开的方式:

$('#manage_images').on('click', '.change_image', function(){
  window.tr = $(this).parents('tr'); 
  var image_id = $(this).data("image_id")

  $('#chage_pro_image_form')
    .find('[name="image_id"]').val(image_id).end();

  $("#modal").modal({
    "backdrop"  : "static",
    "keyboard"  : true,
    "show"      : true
  });
});

Next thing is I want to submit form (which is in the modal) to change this image. 接下来,我要提交表单(以模式形式)以更改此图像。 I did it correctly using jquery/ajax 我使用jquery/ajax正确地做到了

Now I want to replace old image with newly change image on this table row after ajax success. 现在,我想在ajax成功后用此表行上的新更改图像替换旧图像。

This is how I tried it in ajax success function: 这是我在ajax成功函数中尝试过的方法:

success: function(json) {
  var json = JSON.parse(json);        
  if (json.success) {
    $('#modal.modal').modal('hide');
    $('#chage_pro_image_form').formValidation('resetForm', true);

    d = new Date();  
    window.tr.find('img').fadeOut(1000, function() {
      $("img").attr("src", json.image+"?"+d.getTime());
    }).fadeIn(1000);
  } else {
    // my errors   
  }
}

My problem is, its replacing all existing images in the table with new one. 我的问题是,它将表中的所有现有图像替换为新图像。 I created a global variable window.tr . 我创建了一个全局变量window.tr But it doesn't work for me. 但这对我不起作用。

Can anybody help me to figure this out? 有人可以帮我解决这个问题吗? Thank you. 谢谢。

Use this to reference the image instance inside the animation callback 使用this来引用动画回调内的图像实例

window.tr.find('img').fadeOut(1000, function() {
      $(this).attr("src", json.image+"?"+d.getTime());
}).fadeIn(1000);

Well obviously the $("img") selector will select all img elements. 显然,$(“ img”)选择器将选择所有img元素。 You need to somewhere store which button you changed as a variable. 您需要在某个地方存储将哪个按钮更改为变量。 For instance by pointing to the data-image_id attribute. 例如,通过指向data-image_id属性。 Then you can select it using: 然后您可以使用以下命令选择它:

 $('[data-image_id='+image_id+']') 

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

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