2014-01-22 6 views
0
$(function() { 

    alert($("#fruit").val()); 
    $("#fruit").click(change2()); 

}); 

var apple1 = ("<p>The orange is the fruit of the citrus species Citrus × sinensis in the family Rutaceae. The fruit of the Citrus sinensis is called sweet orange to distinguish it from that of the Citrus aurantium, the bitter orange.</p>"); 
var orange1 = ("<p>LOL I AM AN ORANGE </p>") 
var grape1 = ("<p>A grape is a fruiting berry of the deciduous woody vines of the botanical genus Vitis. Grapes can be eaten raw or they can be used for making wine, jam, juice, jelly, grape seed extract, raisins, vinegar, and grape seed oil.</p>"); 
var strawberry1 = ("<p>The garden strawberry is a widely grown hybrid species of the genus Fragaria. It is cultivated worldwide for its fruit. The fruit is widely appreciated for its characteristic aroma, bright red color, juicy texture, and sweetness.</p>"); 
var cantaloupe1 = ("<p>Cantaloupe refers to a variety of Cucumis melo, a species in the family Cucurbitaceae. Cantaloupes range in size from 500 g to 5 kg. Originally, cantaloupe referred only to the non-netted, orange-fleshed melons of Europe.</p>"); 

function change2() { 
    alert("Test2"); 
    var select = $("#fruit").val(); 
    switch (select) { 
    case "apple": 
     $("#description").html(apple1); 
     alert("apple"); 
     break; 

    case "orange": 
     $("#description").html(orange1); 
     alert("orange"); 
     break; 

    case "grapes": 
     $("#description").html(grape1); 
     alert("grapes"); 
     break; 

    case "cantaloupe": 
     $("#description").html(cantaloupe1); 
     alert.ht("melon"); 
     break; 

    case "strawberry": 
     $("#description").html(strawberry1); 
     alert("straw"); 
     break; 

    case "none": 
     alert("reddit is cool"); 
     $("#description").html("<p> wow wow wow much doge </p>" + apple1); 
     break; 
    } 
    alert("end"); 
} 

#fruit 선택 메뉴가 변경되면 switch 문을 활성화하려고합니다. 어떤 이유로 든 내가 무엇을 시도하든 상관 없다. var로 변경하고 싶습니다. 그래서 우리는 웹 페이지에서 apple을 선택하고 switch 문을 통해 실행하여 var apple1을 출력해야합니다. 디프 풀트를 "none"으로 설정하면 시동시 자동으로 "none"이 실행됩니다. 이것은 단지 테스트 용입니다.jQuery 이벤트에서 함수 호출하기

+1

* "작동하지 않습니다"* : 정확히 무엇을 의미합니까? 무슨 일이 일어나고 어떻게 될 것으로 예상됩니까? 문제를 더 잘 설명할수록, 우리가 당신을 도울 수 있고 다른 사람들이 (미래에) 그들이 똑같은 문제가 있는지를 쉽게 인식 할 수 있습니다. –

+0

드롭 다운 메뉴에서 다른 값을 선택하면 ". does not work"함수/.click 함수가 실행되지 않습니다. – Gibson

답변

0

이것은 이벤트 핸들러 함수를 작성하기위한 구문입니다.

$("#fruit").click(change2);

는 괄호

()를 제거하거나 위와 같은 일의 긴 버전을 작성할 수 있습니다.

$("#fruit").click(function() { 
    change2(); 
}); 
+1

이유를 설명해야합니다. –

관련 문제