所以,基本上我有这个选择/下拉菜单,我使用 AJAX 来检索和创建,尽管当选择一个选项时(所以 onChange)我希望它重定向!
尽管如此,这仍然不起作用,但我在尝试时没有抛出任何错误,并尝试执行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/,它加载得很好。在那个栏上,您可以对热门 | 进行“排序”最新 |类别 |学校
如果您将鼠标悬停在学校上,则会出现一个下拉菜单,选择任何国家/地区,然后会出现另一个国家/地区,但更改后不会发生任何情况。
这是第二个下拉菜单的 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;
}
任何帮助都会很棒!
编辑:此外,我在菜单中使用这些下拉菜单的方式有点奇怪,当您将鼠标悬停在任何其他“排序依据”链接上时,它们就会消失,这是“按学校排序”的问题,因为第一个选择框显示列表,如果您选择一所学校,然后以某种方式漂浮在另一个链接上,它就会消失,有关如何延迟该过程或解决该小问题的任何帮助吗?
最佳答案
这是因为页面加载时 #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。
关于javascript - AJAX 生成的选择不会重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4632531/