2012-10-11 227 views
1

我试图在窗体中进行级联选择,但我无法使其工作。看来我的select元素中的onChange()不会触发。onChange()不会触发

php的

$flight_sql = "SELECT * FROM wp_flr_flights"; 
$flight_result = mysql_query($flight_sql); 
echo '<select name="location" id="select-location onChange="changeSecond(this.value)">'; 
echo '<option value="">Select</option>'; 
while ($flt_loc_rows = mysql_fetch_assoc($flight_result)) { 
    $loc_id = $flt_loc_rows["location_id"]; 
    $loc_name = $flt_loc_rows["location_name"]; 
    echo '<option value="'.$loc_id.'">'.$loc_name.'</option>'; 
} 
echo '</select>'; 

的JavaScript

function changeSecond(first){ 
alert('dasda'); 
var xmlhttp; 
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} else {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function(){ 
    if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
     var res=xmlhttp.responseText; 
     document.getElementById("second").innerHTML=res; 
    } 
} 
xmlhttp.open("GET","reservations-plane.php?first="+first,true); 
xmlhttp.send(); 
} 

保留-plane.php

global $wpdb, $page_url; 
    $location=mysql_real_escape_string($_REQUEST["first"]); 
    echo $userid.'ddd'; 
    $flight_sql = "SELECT ".$location." FROM wp_flr_flights GROUP BY ".$location; 
    $flight_result = mysql_query($flight_sql); 
    echo '<select name="plane_type_choose">'; 
    while ($flt_plane_rows = mysql_fetch_row($flight_result)) { 
     $plain_id = $flt_plane_rows["plane_id"]; 
     $plane_type = $flt_plane_rows["plane_type"]; 
     echo '<option value="'.$flt_plane_rows[0].'">'.$flt_plane_rows[0].'</option>'; 
    } 
    echo '</select>'; 
+3

'$ function changeSecond(first){'只是复制/粘贴错字?因为它应该是函数changeSecond(...'。 – Zeta

+1

它看起来你错过了你的id属性后的结尾quoutes(onchange触发器之前) – jtheman

+0

当问一个Javascript问题时,显示'php'或''''''''''''''''''''我们唯一需要看到的是呈现的'HTML'和'Javascript'.也就是说,看起来像其他评论可以解决您的问题 –

回答

4

您在代码中有一个拼写错误:

$function ...应该是function

function changeSecond(first){ 
    ... 
} 

注意:检查您的错误代码

也有生产在HTML中的错误。

echo '<select name="location" id="select-location onChange=....>'; 

对于id属性缺少结尾双引号。

+0

是的$只是一个错字,很好地捕捉到了这个ID,导致onChange无法工作。级联选择仍然不起作用,我不明白为什么这是如此困难 – gilgimech

+0

@gilgimech然后缺少双引号必须是问题 – Ibu

相关问题