window.history.back()

Asked

Viewed 185 times

-1

I have a page that has a form with some data and another later page that has a back button, the idea is that when the user uses this button he does not lose the data that was entered in the form.

The problem is that the data that the user typed is being kept but the data that was loaded via the javascript "trigger" goes blank, for example.

I have a select field that has a onchange:

select name="cnpj" onchange="getnome(this.value);"

in that capacity getnome I search the name in a list and exchange the value through the command below

document.getElementsByName('nome')[0].value = aNomes.substr(posCnpj + 15, 30);

the name is changed successfully but when the user uses the back button this field is empty, as if it had not been filled.

this only happens with the fields that were loaded in the javascript is with change via .value or setAttribute, I’ve tried both ways

to get back I’m using the windows.history.back()

someone knows how I can adjust it?

thank you very much!

follow the complete codes below:

index1.php

<!DOCTYPE html>
<html lang="en" class="no-js">

<head>
    <script type="text/javascript" src="js/js.js"></script>
</head>

<body>

    <form name="form1" id="form1" method="post" enctype="multipart/form-data" action="index2.php">

        <select name="cnpj" onchange="getnome(this.value);"><option value="0"></option><option value="1">Teste1</option><option value="2">Teste2</option></select>
        <input type="text" name="nome" readonly="readonly" size="100">

        <button id="calcular" type="submit" size="5%" class="btn btn-success">Calcular orçamento</button>

    </form>

</body>

</html>

index2.php

<!DOCTYPE html>
<html lang="en" class="no-js">

<head>
    <script type="text/javascript" src="js/js.js"></script>
</head>

<body>

    <form name="form1" id="form1" method="post" enctype="multipart/form-data">


        <button type="button" onclick="window.history.back()" class="btn btn-primary">Voltar</button>

    </form>

</body>

</html>

js.js

function getnome(valor) {

    document.getElementsByName('nome')[0].value = "teste";


}
  • I’m voting to close because I couldn’t reproduce the problem. Even trying to change the value and then using "window.history.back()" all values are maintained. There is code in your question that presents exactly the problem.

  • This is Inkeliz, thanks for the interaction, I just finished editing the codes.

  • I just found out, the problem occurs when the field is as readonly, know if there is any way to leave as read only without losing information on the return?

1 answer

0


To those with the same problem the solution was to remove the readonly of the fields and did the lock via css as below:

input[name="nome"] {
  pointer-events:none;
  outline:none;
  background:#eee;
  border:1px solid #ccc;
}

Thus returning to the previous page the values were maintained.

Thank you!

Browser other questions tagged

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