I have a dialog ($('.dialog').show()
), that writes a form ($.post('/form/xyz', null, function (data) { $('.dialog').html( data );} )
). That form has an script (javascript/jquery) like this:
<script type="text/javascript" src="myform/script.js">
<form>
<input type="text" value="click me!" id="clickme" />
</form>
The script has the following code:
$(document).ready( function () {
$(document).on('click','#clickme', function () { alert('you clicked me'); } );
});
The problem is: each time the dialog is showed I need to re-execute the script but when I click #clickme
I get the alert showed the times that the script was executed.
I never noted this problem (I don't know why) but now that is happening. I'm working with jQuery 1.9.2, and I'm thinking to use the function (preventPropagation
), but I think this isn't reliable because I should need to do this at each 'on' event. In addition, I believed that doing an 'script loading history' will solve the problem to control that, but I have the problem that when I need to execute functions when the form is loaded I will cannot re-execute automatically as well as I'm doing now.
What's the solution?
Full example:
HTML
<html>
<head>
<script type="text/javascript">
$(document).ready( function () {
$('#open-dialog').on('click', function () {
$.post( '/server-form.php', null, function (data) {
$('.dialog').html( data );
});
});
});
</script>
</head>
<body>
<div class="dialog">
</div>
<input type="button" value="Open dialog" id="open-dialog"/>
</body>
</html>
PHP (server-form.php)
<script type="text/javascript" src="dynamic-script/30a2f63d6276a21db19782b2d8c93363.js">
<form>
<input type="text" value="click me!" id="clickme" />
</form>
*DYNAMIC-SCRIPT dynamic-script/30a2f63d6276a21db19782b2d8c93363.js *
$(document).ready( function () {
$(document).on('click','#clickme', function () { alert('you clicked me'); } );
});
That's all!!!