我已經得到它的工作到第三個選擇列表。但有4個。4級相互依賴的選擇列表ajax jquery json和mysql
國家,來源,目標和主題。
我能夠選擇到目標選擇列表好,但我不能讓它到最後一個主題。這是因爲我不知道如何完成那個缺失的代碼。
這裏是腳本,下面將進一步處理。因此,它是一個需要相應地移動到它旁邊的左側的「主題」,選擇列表:
jQuery(document).ready(function() {
jQuery("#sel_pais").change(function() {
//
var datatosend = 'pais_id=' + jQuery(this).val();
jQuery.ajax({
type:'GET',
url:'includes/getchilds2.php',
data:datatosend,
dataType:"json",
success:function (data) {
jQuery('#sel_source').find('option').remove().end();
jQuery('#sel_target').find('option').remove().end();
jQuery.each(data, function (index, val) {
var newopt = '<option value="' + val.key + '">' + val.title + '</option>';
jQuery('#sel_source').append(newopt);
});
jQuery('#sel_target').append('<option value="-1">Select</option>');
}
});
});
//////////////////////
jQuery("#sel_source").change(function() {
//
var datatosend = 'id_from=' + jQuery(this).val();
jQuery.ajax({
type:'GET',
url:'includes/getchilds2.php',
data:datatosend,
dataType:"json",
success:function (data) {
jQuery('#sel_target').find('option').remove().end();
jQuery.each(data, function (index, val) {
var newopt = '<option value="' + val.key + '">' + val.title + '</option>';
jQuery('#sel_target').append(newopt);
});
}
});
});
});
這裏是處理一個:
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("traducteurs", $con);
//
if (isset($_GET['pais_id'])) {
$curid = $_GET['pais_id'];
//
$result = mysql_query("SELECT * FROM tabla_from where pais_id=" . $curid);
if (mysql_num_rows($result) > 0) {
$op = '[';
$op .= '{"title":"Select","key":"-1"},';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$op .= '{';
$op .= '"title":"' . $row['from_name'] . '", "key":"' . $row['id_from'] . '"';
$op .= '},';
}
$op = substr($op, 0, -1);
$op .= ']';
echo $op;
}
else {
echo '[{"title":"Select","key":"-1"}]';
}
}
/////////////////////
if (isset($_GET['id_from'])) {
$curid = $_GET['id_from'];
//
$result = mysql_query("SELECT * FROM tabla_into where id_from=" . $curid);
if (mysql_num_rows($result) > 0) {
$op = '[';
$op .= '{"title":"Select","key":"-1"},';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$op .= '{';
$op .= '"title":"' . $row['into_language'] . '", "key":"' . $row['id_into'] . '"';
$op .= '},';
}
$op = substr($op, 0, -1);
$op .= ']';
echo $op;
}
else {
echo '[{"title":"Select","key":"-1"}]';
}
}
?>
請不要在新代碼中使用'mysql_ *'函數。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-20 16:40:32
請不要親自接受它,這是一個自動註釋,當我在一個問題中看到'mysql_ *'時立即拋出。如果你已經意識到這個代碼是可怕的,那麼你已經拯救了另一隻小貓。 – 2012-07-20 16:46:19