2011-01-08 37 views
1

所以,基本上我有這個選擇/下拉菜單,我使用AJAX檢索和創建,但是當選擇一個選項(so onChange)時,我希望它重定向!AJAX生成的選擇不會重定向

雖然這仍然不起作用,但在嘗試時沒有遇到任何錯誤,並嘗試執行alert()調試方法,但警報沒有被調用。

jQuery的

$("#schoolMenu").change(function() { 
    option = $("#schoolMenu option:selected").text(); 

    alert(option); 

    if(option != "- Please Select -") { 
    window.location = "http://www.itmustbecollege.com/pics/pics-from-"+$("#schoolMenu option:selected").val(); 
    } 
    }); 

這就是用來調用AJAX

// // 填充學校 //

$("#state").change(function() { 
    option = $("#state option:selected").text(); 

    if($(this).attr("class") == "menu") { 
    if(option != "- Please Select -") { 
    $.ajax({ 
    type: "GET", 
    url: "/includes/functions.php", 
    data: "f=school&p="+option+"&m=yes", 
    success: function(msg) { 
     $("#fSchool").html("<p style=\"margin-left: 20px;\">Select School:</p>"+ msg); 
     $("#fSchool").show(); 
     $("#school").focus(); 
    } 
    }); 
    } 
    else { 
    $("#fSchool").html(""); 
    $("#fSchool").hide(); 
    } 
    } 
    else { 
    if(option != "- Please Select -") { 
    $.ajax({ 
    type: "GET", 
    url: "/includes/functions.php", 
    data: "f=school&p="+option, 
    success: function(msg) { 
     $("#fSchool").html(msg); 
     $("#fSchool").show(); 
     $("#school").focus(); 
    } 
    }); 
    } 
    else { 
    $("#fSchool").html(""); 
    $("#fSchool").hide(); 
    } 
    } 
    }); 

它加載完美,如果你看一下http://www.itmustbecollege.com/quotes/在那裏你可以按照Popular |排序最新|類別|學校

如果你懸停在學校的下拉菜單出現,選擇任何國家和另一個出現,雖然當改變什麼都沒有發生。

這裏是PHP對於第二個下拉

// Get College List 
function getCollege($state, $m = "no", $l = "no") { 

// Displays Schools 
if($m == "yes") { 
$options = '<select id="schoolMenu" name="school"><option value="select" selected="selected">- Please Select -</option>'; 
} 
else if($l == "yes" || $l == "yes2") { 
$options = ''; 
} 
else { 
$options = '<select name="school"><option value="select" selected="selected">- Please Select -</option>'; 
} 

$listArray = split("\|\\\\", $list); 

for($i=0; $i < count($listArray); $i++) { 

if($m == "yes") { 
    $options .= '<option value="'. trim(titleReplace($listArray[$i])) .'">'. trim($listArray[$i]) .'</option>'; 
} 
else if($l == "yes") { 
    $options .= '<li><a href="/quotes/quotes-from-'. titleReplace($listArray[$i]) .'" title="'. trim($listArray[$i]) .' Quotes">'. trim($listArray[$i]) .'</a></li>'; 

} 
else if($l == "yes2") { 
    $options .= '<li><a href="/pics/pics-from-'. titleReplace($listArray[$i]) .'" title="'. trim($listArray[$i]) .' Pictures">'. trim($listArray[$i]) .'</a></li>'; 

} 
else { 
    $options .= '<option value="'. trim($listArray[$i]) .'">'. trim($listArray[$i]) .'</option>'; 
} 
} 

echo $options .='</select>'; 
return false; 
} 

任何幫助將是巨大的!

編輯:另外,我有那些下拉菜單的方式有點奇怪,當你懸停在任何其他「排序」鏈接他們消失,這是「按學校排序」的問題,因爲第一個選擇框顯示列表,如果你去選擇一所學校,然後以某種方式漂浮在它消失的另一個鏈接上,如何延遲或修復那個小問題的任何幫助?

回答

1

這是因爲#schoolMenu元素不存在時,頁面加載,所以你.change()處理程序不會得到分配。

您可以在它到達時進行分配。

var fSchool = $("#fSchool").html("<p style=\"margin-left: 20px;\">Select School:</p>"+ msg); 

fSchool.find("#schoolMenu").change(function() { 
    option = $(this).find("option:selected").text(); 

    alert(option); 

    if(option != "- Please Select -") { 
    window.location = "http://www.itmustbecollege.com/pics/pics-from-"+$("#schoolMenu option:selected").val(); 
    } 
}); 

fSchool.show(); 

注意,我在fSchool變量緩存$("#fSchool"),而不是重複地從DOM中選擇。

而且.change()處理程序中,我引用收到使用this代替"#schoolMenu"的情況下,再次以避免重複選擇的DOM元素。

+0

在這種情況下,直播或代表不起作用? – 2011-01-08 06:30:36