Trois select liées jquery
Bonjour , cela n'est pas de moi c'est un script pays régions départements j'aimerais le faire fonctionner , le soucis c'est que les régions et les départements ne s'affiche pas , voici le code php et jquery .Merci de votre aide peut-être rajouter avec mysql et le php aussi
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?php
$array = array();
if ($_GET['_name'] == 'country')
{
if ( $_GET['_value'] == 3 )//usa
{
$array[] = array('1' => 'New York');
$array[] = array('2' => 'Montana');
$array[] = array('3' => 'Texas');
} else
{
$array[] = array('0' => 'No state');
}
} elseif ($_GET['_name'] == 'state')
{
if ( $_GET['_value'] == 2 )//New York
{
$array[] = array('1' => 'New York');
$array[] = array('2' => 'Another city');
} else
{
$array[] = array('0' => 'No city');
}
} else
{
$array[] = array('1' => 'Data 1');
$array[] = array('2' => 'Data 2');
$array[] = array('3' => 'Data 3');
}
echo json_encode( $array );
?> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chained comboboxes demo</title>
<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script language="JavaScript" type="text/javascript" src="jquery.chainedSelects.js"></script>
<script language="JavaScript" type="text/javascript">
$(function()
{
$('#country').chainSelect('#state','combobox.php',
{
before:function (target) //before request hide the target combobox and display the loading message
{
$("#loading").css("display","block");
$(target).css("display","none");
},
after:function (target) //after request show the target combobox and hide the loading message
{
$("#loading").css("display","none");
$(target).css("display","inline");
}
});
$('#state').chainSelect('#city','combobox.php',
{
before:function (target)
{
$("#loading").css("display","block");
$(target).css("display","none");
},
after:function (target)
{
$("#loading").css("display","none");
$(target).css("display","inline");
}
});
});
</script>
<style>
#loading
{
position:absolute;
top:0px;
right:0px;
background:#ff0000;
color:#fff;
font-size:14px;
font-familly:Arial;
padding:2px;
display:none;
}
</style>
</head>
<body>
<div id="loading">Loading ...</div>
<h1>Chained select demo</h1>
<form name="formname" method="post" action="">
<!-- country combobox -->
<select id="country" name="country">
<option value="1" selected>France</option>
<option value="2">Romania</option>
<option value="3">Usa</option>
<option value="4">Brazil</option>
</select>
<!-- state combobox is chained by country combobox-->
<select name="state" id="state" style="display:none"></select>
<!-- city combobox is chained by state combobox-->
<select name="city" id="city" style="display:none"></select>
</form> |
et enfin le jquery
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| jQuery.fn.chainSelect = function( target, url, settings )
{
return this.each( function()
{
$(this).change( function( )
{
settings = jQuery.extend(
{
after : null,
before : null,
usePost : false,
defaultValue : null,
parameters : {'_id' : $(this).attr('id'), '_name' : $(this).attr('name')}
} , settings);
settings.parameters._value = $(this).val();
if (settings.before != null)
{
settings.before( target );
}
ajaxCallback = function(data, textStatus)
{
$(target).html("");//clear old options
data = eval(data);//get json array
for (i = 0; i < data.length; i++)//iterate over all options
{
for ( key in data[i] )//get key => value
{
$(target).get(0).add(new Option(data[i][key],[key]), document.all ? i : null);
}
}
if (settings.defaultValue != null)
{
$(target).val(settings.defaultValue);//select default value
} else
{
$("option:first", target).attr( "selected", "selected" );//select first option
}
if (settings.after != null)
{
settings.after(target);
}
$(target).change();//call next chain
};
if (settings.usePost == true)
{
$.post( url, settings.parameters, ajaxCallback );
} else
{
$.get( url, settings.parameters, ajaxCallback );
}
});
});
}; |