I recently began to use the jQuery framework. But this morning I encountered a problem. I do not know how to run a function on multiple events. In my form I would like to make a function on a click in a select or keyup on an input text. To solve my problem I went through a function and I call this function on my click and select onkeyup input in my area. I wonder if it is possible to avoid these function calls in the html code with jquery?
Code which is not OK. My code does not fire on the keyup event as I would like keyup on id_coclico and click on the country id.
PHP Code:
$ (document). ready (function () {
var id = $ ( '# id_coclico, #country');
$. each (id, function () {
$ (this ).keyup(function(event){
coclico = $("#id_coclico" ).val();
country = $ ( "# country"). val ();
$.post('ajax/ajax_test_coclico.php',{
coclico:coclico,country: country)
function(data){
if (data == '1 ') {
$ ( "#test_nok"). hide ();
$('#test_ok').fadeIn("slow", 0, function(){
$ ("#test_ok"). show ();
});
}
else
{
$ ( "# test_ok"). hide ();
$ ( '# test_nok'). FadeIn ( "slow", 0, function () {
$ ( "# test_nok"). show ();
});
}
});
});
});
});
Code is ok but not really what I want because I have to call a function in my form:
PHP Code:
function test_client(){
var id = $('#id_coclico,#country');
$.each(id, function() {
coclico = $("#id_coclico" ).val();
country= $("#country" ).val();
$.post('ajax/ajax_test_coclico.php',{
coclico:coclico,country:country},
function(data){
if (data=='1'){
$("#test_nok" ).hide();
$('#test_ok').fadeIn("slow", 0, function(){
$("#test_ok" ).show();
});
}
else
{
$("#test_ok" ).hide();
$('#test_nok').fadeIn("slow", 0, function(){
$("#test_nok" ).show();
});
}
});
});
}
Bookmarks