2012-07-20 62 views
1

我已经得到它的工作到第三个选择列表。但有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"}]'; 
    } 
} 
?> 
+0

请不要在新代码中使用'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

+0

请不要亲自接受它,这是一个自动注释,当我在一个问题中看到'mysql_ *'时立即抛出。如果你已经意识到这个代码是可怕的,那么你已经拯救了另一只小猫。 – 2012-07-20 16:46:19

回答

1

我使用此代码显示相互依赖列表

<select id="sel_pais" name="sel_pais"> 
<option value="pais_id">values goes here</option> 
</select> 

<select id="sel_source" name="sel_source"> 
<option value="">Select</option> 
</select> 

<select id="sel_target" name="sel_target"> 
<option value="">select</option> 
</select> 

<select id="sel_subject" name="sel_subject"> 
<option value="">select</option> 
</select> 
//jquery code for source list 
$('#sel_pais').change(function() { 
    if ($(this).val()!='') { 
    $("#sel_source").load("include/getchild2.php",{pais_id: $(this).val()}); 
    } 
}); 
//code on change of sel_source 
$('#sel_source').change(function() { 
    if ($(this).val()!='') { 
    $("#sel_target").load("include/getchild2.php",{id_from: $(this).val()}); 
    } 
}); 

$('#sel_target').change(function() { 
    if ($(this).val()!='') { 
    $("#sel_subject").load("include/getchild2.php",{id_target: $(this).val()}); 
    } 
}); 

php示例代码,您可以更改..重复下面的代码为他人。

if(isset($_REQUEST['pais_id']) && !empty($_REQUEST['pais_id'])) { 
    $result = mysql_query("select * from table where id='".$_REQUEST['pais_id']."' ")); 
    if(mysql_num_rows($result) > 0) { 
     echo '<option value="">Select</option>'; 
     while($row = mysql_fetch_assoc($result)) {    
       echo '<option value="'.$row['id'].'">'.$row['name'].'</option>'; 
     } 
    } else { 
     echo '<option value="">Select</option>'; 
    } 
} 

希望这能帮到你。

+0

非常感谢你,我现在要去研究它。这需要我一段时间,因为我是新手,但我会回来,感谢和反馈。问候 – iaintunderstand 2012-07-20 17:12:49

+0

好吧,我已经研究过它。我喜欢它,因为它比我所使用的代码更清晰。我会尝试它,并会回来。一开始我给你的感谢是一个有用的答案,而且当我实施它时,会告诉你我是否设法使其工作。非常感谢你! – iaintunderstand 2012-07-20 17:18:50

+0

ü欢迎..它为你工作? – 2012-07-20 17:54:41