Change the image src

Asked

Viewed 12,576 times

2

I have a field called img, in it I save the image name, 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, deletes the previous image and saves the new one with the same name: 001.jpg

That’s where the problem is: I change the attribute src via jQuery, is working because I put a test and changed the name, but the name is not changed and only the image in the folder.

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

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

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

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

Am I logical or missing something?

What I’ve already 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('');
  }
});

1 answer

4

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

// 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>
<img id='imagem' src='001.jpg' alt='imagem1'>
<br>
<img id='imagem2' src='001.jpg' alt='imagem2'>
<br>
<div id="imagemDinamica">
</div>

  • My goal is to show the new image that has just been saved. When I save the image I return to the ajax only the name and change the attr src. that is the system change 001.jpg to 001.jpg, but the image is different. I didn’t understand what you posted, could give me a light ?

  • Do you need to save the image by putting an id on it and then show it? You can put the code you’ve already done to understand its logic ?

  • Delete the comment and edit your post by placing your code you can use <pre> code </pre> to highlight and indent

  • The save code is only the Upload and then I give a Sponse that is caught by ajax <pre> $. 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('); } }); </pre>

  • I cannot use your example 1 because the "00" are not fixed, it increases. in my code has the result variable, in it I return the image name, so I’m already doing your example 2, agree ? and in both cases did not solve, the script changes the name of the image, but my problem is that before it had an image and now it is another figure but with the same name.

  • Instead of getting Id Get the name of the whole picture

  • but I’m not taking the ID, just an example, I’m taking the name of the image to show in the result

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.