I have this code
<html>
<include jquery>
<script>
function crea()
{
var html = '<form method="get" id="popUpForm" name="popUpForm" action="form_ricorda_dati.php"><hr /><input type="hidden" name="mio" value="1" />input3<input type="text" name="input3" value="" /><br />input4<input type="text" name="input4" value="" /><br /><input type="submit" id="11" value="Procedi" /></form><br /><a href="" onClick="prova()">submit</a>';
var div = document.getElementById('cont');
div.innerHTML = html;
}
function prova()
{
$('#popUpForm').submit();
}
</script>
<body>
< a href="#" onClick="crea()">lancia funzione JS</a><br /><br />
<div id="cont"></div>
</body>
</html>
This code:
- When I click on
<a href="#" onClick="crea()">it “shows” the form
into the<div id="cont"> - Way 1: When I click on
<a href=""it calls
onClick="prova()">submit</a>$('#popUpForm').submit();
Way 2: Click on<input type="submit" id="11" value="Procedi"
/>
Problem:
-
If I click
<input type="submit" id="11" value="Procedi" />I
reload the page and see correct query string (formaction="get"). In
the reloaded page, if I “show” the form and click on the input I see the last
input (browser autofill okay). -
If I click
<a href="" onClick="prova()">submit</a>, after, I
don’t see the query string. In the reloaded page if I “show” the form and
click on the input I don’t see the last input (browser autofill fails).
(I see this problem in Chrome and IE, but not in Firefox.)
Goal
The browser autofill should always show.
The problem might be that the HTML form you are creating with JavaScript was not in the browser’s DOM at the moment the page was loading. You can fix this by placing the form’s HTML into your
bodytag to be rendered in the DOM, and hide it with JavaScript until a user clicks thecrealink.The problem number 2 seems to be that your link (
<a href="" onclick="prova()"...) might go to the url given in theHREFattribute. Since that attribute is empty, your page goes to itself.To make things easier, you should keep your HTML form out of your JavaScript logic. Also since you are using jQuery, I have converted your functions to binds.
Working example in JSFIddle.