2011-01-08 4 views
1

그래서 기본적으로이 AJAX를 사용하여 검색 및 작성하는 선택/드롭 다운 메뉴가 있는데, 옵션을 선택하면 (그렇게 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/에서 "정렬 기준"을 수행 할 수있는 바 | 최신 | 카테고리 | 학교

학교로 마우스를 가져 가면 드롭 다운 메뉴가 나타나면 아무 나라 나 다른 국가가 선택됩니다. 아무 변화가 없더라도 아무 것도 나타나지 않습니다.

여기

// 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; 
} 

도움이 아래로 그 두 번째 드롭에 대한 PHP가 좋은 것입니다!

편집 : 또한 드롭 다운 메뉴가 나오는 방식이 조금 이상합니다. 다른 "정렬 기준"링크 위로 마우스를 가져 가면 사라집니다. 이는 "학교 별 정렬"과 관련된 문제입니다. 첫 번째 선택 상자는 목록을 보여줍니다. 학교에 가서 학교를 선택하면 사라지는 다른 링크를 통해 어떻게 든 떠 다니고, 지연시키는 방법에 대한 도움이나 사소한 문제를 해결할 수 있습니까?

답변

1

페이지가로드 될 때 #schoolMenu 요소가 없기 때문에 .change() 처리기가 할당되지 않습니다.

도착하면 할당 할 수 있습니다. 내가 대신 반복적으로 DOM에서 선택의의 fSchool 변수에 $("#fSchool") 캐시

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(); 

참고.

그리고 .change() 처리기 내부

는 I 다시 반복 피하기 위해 DOM 선택 this 대신 "#schoolMenu"를 이용하여 이벤트를 수신 요소를 참조.

+0

이 경우 라이브 또는 대리인이 작동하지 않습니까? –

+1

@Caspar : 좋은 질문입니다. 작동하는 것처럼 보이지만 어느 방법 으로든 특정 요소를위한 특정 처리기이므로 개인적으로 직접 바인딩하는 것을 선호합니다. – user113716

관련 문제