2009-09-16 38 views
8

我想知道我的方法是否有效且正確。我的代碼雖然不工作,但我不知道爲什麼。在jquery和列表中的switch語句

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script> 

<script type="text/javascript"> 

$(document).ready(function() { 


    function HotelQuery(HotelName) { 
    switch (HotelName) { 
    case 'TimelessHotel': 
    var strHotelName = 'Timeless Hotel'; 
    var strHotelDesc = 'Hotel Description Timeless Hotel'; 
    var strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    var strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    ; //end Timeless Hotel 

    case 'ParadiseInn': 
    var strHotelName = 'Paradise Inn'; 
    var strHotelDesc = 'Hotel Description Paradise Inn'; 
    var strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    var strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    ; //end Paradise Inn 

    case 'TetrisHotel': 
    var strHotelName = 'Tetris Hotel'; 
    var strHotelDesc = 'Hotel Description Tetris Hotel'; 
    var strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    var strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    ; //end Tetris Hotel 

    case 'JamstoneInn': 
    var strHotelName = 'Jamstone Inn'; 
    var strHotelDesc = 'Hotel Description Jamstone Inn'; 
    var strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    var strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    ; //end Jamstone Inn 

    } 
    }; 


}); 

    </script>  

<title>hotel query</title> 
</head> 

<body> 

    <a href="#" onclick="javascript: HotelQuery('TetrisHotel'); alert: (strHotelName, strHotelDesc, strHotelPrice);">Tetris Hotel Query</a> 

</body> 
</html> 

回答

26

由於變量的作用域爲HotelQuery,因此您的代碼無效。我認爲你可能想要做的是從函數中返回一個包含屬性的對象,並且使用不顯眼的JavaScript方法將點擊事件處理程序綁定到<a>元素。

喜歡的東西

$(function() { 
    $('a').click(function() { 
     var hotel = HotelQuery('TetrisHotel'); 

     alert(hotel.name) // alerts 'Tetris Hotel' 
    }); 
}); 

function HotelQuery(HotelName) { 
    var strHotelName; 
    var strHotelDesc; 
    var strHotelPrice; 
    var strHotelRoomType; 

    switch (HotelName) { 
     case 'TimelessHotel': 
      strHotelName = 'Timeless Hotel'; 
      strHotelDesc = 'Hotel Description Timeless Hotel'; 
      strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
      strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person']; 
      break; //end Timeless Hotel 

     case 'ParadiseInn': 
      strHotelName = 'Paradise Inn'; 
      strHotelDesc = 'Hotel Description Paradise Inn'; 
      strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
      strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
      break; //end Paradise Inn 

     case 'TetrisHotel': 
      strHotelName = 'Tetris Hotel'; 
      strHotelDesc = 'Hotel Description Tetris Hotel'; 
      strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
      strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person']; 
      break; //end Tetris Hotel 

     case 'JamstoneInn': 
      strHotelName = 'Jamstone Inn'; 
      strHotelDesc = 'Hotel Description Jamstone Inn'; 
      strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
      strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
      break; //end Jamstone Inn 
    } 
    return { 
     name: strHotelName, 
     desc: strHotelDesc, 
     price: strHotelPrice, 
     roomType: strHotelRoomType 
    } 
}; 

只注意到你也比返回酒店的名稱和說明每次(你可能會做這只是作爲一個例子等相同的價值觀,我不知道)。您可以將所有變量的值分配給聲明(或將值分配爲返回對象的屬性),而不是酒店名稱和描述,您可以從參數HotelName的參數值中指定該值。像

function hotelQuery(hotelName) { 
    return { 
     name: hotelName, 
     desc: 'Hotel Desciption' + hotelName, 
     // Keep prices as numbers and have a function to display them 
     // in the culture specific way. Numbers for prices will be easier to deal with 
     price: [980, 1300, 1600, 1500, 1800, 300, 150, 200], 
     roomType: ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'] 
    } 
} 
+1

查看Crockford的偉大文章「JavaScript編程語言綜述」 - http://javascript.crockford.com/survey.html注意:這可能不適合絕對的初學者。 – 2009-09-16 20:07:26

0

東西有幾個變化,我會做。

ready函數中取出HotelQuery函數。

其次,所有這些變量將在您進行警報呼叫時超出範圍。如果你想讓它們在範圍內,則可以在全局範圍內聲明它們(在你的函數之外)並將它們放入函數中。

var name; 

function doStuff() { 
    name = "reggie"; 
} 
8

幾個問題。

1)功能不需要在$(document).ready之內,擺脫那個。


2)每個case語句應遵循的一個break,而不是一個孤獨的;。例如:

function HotelQuery(HotelName) { 
    switch (HotelName) { 
    case 'TetrisHotel': 
     // stuff goes here ... 
     break; //end Tetris Hotel 
    }; 
} 

3)alert不應該跟一個:onclick處理程序:

alert: (strHotelName, strHotelDesc, strHotelPrice); 

應該

alert(strHotelName, strHotelDesc, strHotelPrice); 

此外,alert只需要一個參數,所以你需要分解它:

alert(strHotelName); alert(strHotelDesc); alert(strHotelPrice); 

3)你假設strHotelNamestrHotelDescstrHotelPrice是在全球範圍內,他們都沒有。


總之,你可能想嘗試這樣的事:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script> 

<script type="text/javascript"> 

    function HotelQuery(HotelName) { 
    var response = { 
     strHotelName: '', 
     strHotelDesc: '', 
     strHotelPrice: [], 
     strHotelRoomType: [] 
    }; 
    switch (HotelName) { 
    case 'TimelessHotel': 
    response.strHotelName = 'Timeless Hotel'; 
    response.strHotelDesc = 'Hotel Description Timeless Hotel'; 
    response.strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    response.strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    break; //end Timeless Hotel 

    case 'ParadiseInn': 
    response.strHotelName = 'Paradise Inn'; 
    response.strHotelDesc = 'Hotel Description Paradise Inn'; 
    response.strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    response.strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    break; //end Paradise Inn 

    case 'TetrisHotel': 
    response.strHotelName = 'Tetris Hotel'; 
    response.strHotelDesc = 'Hotel Description Tetris Hotel'; 
    response.strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    response.strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    break; //end Tetris Hotel 

    case 'JamstoneInn': 
    response.strHotelName = 'Jamstone Inn'; 
    response.strHotelDesc = 'Hotel Description Jamstone Inn'; 
    response.strHotelPrice = ['980.00', '1,300.00', '1,600.00', '1,500.00', '1,800.00', '300.00', '150.00', '200.00']; 
    response.strHotelRoomType = ['Single Room', 'Delux Room','Twin Room', 'Matrimonial Room', 'Presidential Suites', 'Extra Bed', 'Free Breakfast', 'Extra Person'];  
    break; //end Jamstone Inn 
    } 

    return response; 
    }; 

    $(document).ready(function() { 
     var infoContainer = $('#hotel-information'); 
     $("#hotel-query").click(function() { 
      var info = HotelQuery('TetrisHotel'); 
      infoContainer.text(info.strHotelName); 
     }); 
    }); 
    </script>  

<title>hotel query</title> 
</head> 

<body> 
    <a href="#" id="hotel-query">Tetris Hotel Query</a> 
    <p id="hotel-information"></p> 
</body> 
</html> 
2
alert("myVar1= " + myVar1 +"/n"+ "myVar2= " + myVar2); 

配售/ N在字符串中的側一個警告框,讓您可以顯示漂亮的線多瓦爾在警告框中打破。

myVar1= Data 
myVar2= more Data 
+0

/n或你的意思是\ n? – curtisk 2012-12-13 16:50:52