Change image src

2

I have a field that calls img, in it I record the name of the image, for example: 001.jpg, where "1" corresponds to the record ID.

I have a routine that changes the image and is working, the change is made by jQuery and ASP.

But when I change the image, the routine saves with the same name, that is, delete the previous image and save the new one with the same name: 001.jpg

This is where the problem is: I get the src attribute via jQuery, it's working because I put a test and changed the name, however the name is not changed and only the image in the folder.

That is, as src is not changed the image also does not change.

And if I change the name of the image in the database gives the same, why imagine the situation:

figura atual: 001.jpg
figura nova : 001-x.jpg

There the last one was in the database and when I make the change, it will be changed again to 001-x.jpg or it will never change the src attribute.

Am I in the right logic or is something missing?

What I've done:


$.ajax({
  url: "usuarios-acoes.asp?IdReg=" + IdReg + "&Acao=" + Acao + "&nomeUsu=" + nomeUsu,
  dataType: "html",
  success: function(result) {
    if (result == "N") {} else {
      $('#IdUsuarioImg').val('');
      $('#titNomeModalImg').html('');
      $('#img-peq-usu-' + IdReg).attr('src', result);
      $('#img-gde-usu-' + IdReg).attr('src', result);
    }
  },
  error: function() {
    $('#IdUsuarioImg').val('');
    $('#titNomeModalImg').html('');
  }
});

    
asked by anonymous 25.07.2015 / 00:01

1 answer

4

If you change the src and what you need you can use example 1, if you change the selector example 2 and if you are going to create example 3 dynamically.

// 1 Para mudar o caminho da imagem
var id = 3;
var test = $("#imagem").attr('src', '00' + id + '.jpg');
console.log(test);

// 2 Para mudar o Seletor da imagem
var test2 = $('#imagem' + id).attr("src", "001.jpg");
console.log(test2);

// 3 Para criar o seletor e a url Dinamicamente
var test3 = "<img id='imagem" + id + "' src='00" + id + ".jpg' alt='imagem" + id + "'><br>";
$("#imagemDinamica").append(test3);
console.log(test3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><imgid='imagem'src='001.jpg'alt='imagem1'><br><imgid='imagem2'src='001.jpg'alt='imagem2'><br><divid="imagemDinamica">
</div>
    
25.07.2015 / 00:51