我已经得到它的工作到第三个选择列表。但有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