gen
23
AjaxifyForm per Jquery
postato da Svacant in JavascriptEcco il codice
Codice
jQuery(function($) {
$(‘form’).attr(“onkeypress”, function() {
$(‘#’+this.id).attr(‘onSubmit’,'return false’);
var add_ev = ‘if(event.keyCode == 13){send_form(“‘+this.id+’”,”‘+this.action+’”);}’;
$(‘#’+this.id+’ input:button’).attr(‘onclick’,'send_form(“‘+this.id+’”,”‘+this.action+’”);’);
return add_ev;
});
});
$(‘form’).attr(“onkeypress”, function() {
$(‘#’+this.id).attr(‘onSubmit’,'return false’);
var add_ev = ‘if(event.keyCode == 13){send_form(“‘+this.id+’”,”‘+this.action+’”);}’;
$(‘#’+this.id+’ input:button’).attr(‘onclick’,'send_form(“‘+this.id+’”,”‘+this.action+’”);’);
return add_ev;
});
});
Con questo codice un semplice form così :
Codice
<form id=”myform” action=”registration.php” method=”get”>
<input type=”text” name=”username”/>
<input type=”button” value=”Invia”/>
</form>
<input type=”text” name=”username”/>
<input type=”button” value=”Invia”/>
</form>
Diventa :
Codice
<form id=”myform” onkeypress=”if(event.keyCode == 13){send_form(“myform”,”registration.php”);}” action=”registration.php” method=”get” onsubmit=”return false”>
<input type=”text” name=”username”/>
<input type=”button” value=”Invia” onclick=”send_form(“myform”,”registration.php”);”/>
</form>
<input type=”text” name=”username”/>
<input type=”button” value=”Invia” onclick=”send_form(“myform”,”registration.php”);”/>
</form>
Tutto si riaggancia alla funzione send_form la quale riceve l’id del form e la pagina dove inviare la richiesta.
Ovviamente è molto semplice come codice, questo anche per scelta personale, meglio le cose semplici e funzionali, spero vi sia utile.














