2009-09-16 31 views
8

Tôi muốn biết liệu phương pháp tiếp cận của tôi có hiệu quả và chính xác hay không. mã của tôi không hoạt động, tôi không biết tại sao.tuyên bố chuyển đổi trong Jquery và Danh sách

<!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> 

Trả lời

26

Mã bạn không hoạt động vì các biến được phạm vi chức năng HotelQuery. Tôi nghĩ rằng những gì bạn có thể muốn làm là trả về một đối tượng với các thuộc tính từ hàm, và cũng sử dụng cách tiếp cận JavaScript không phô trương để ràng buộc một trình xử lý sự kiện nhấn vào phần tử <a>.

Something như

$(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 
    } 
}; 

Chỉ cần nhận thấy rằng bạn cũng đang trở về cùng các giá trị khác với tên khách sạn và mô tả mỗi lần (bạn có thể đã làm điều này chỉ là một ví dụ, tôi không chắc chắn). Bạn chỉ có thể gán tất cả các biến giá trị của chúng vào khai báo (hoặc gán các giá trị như các thuộc tính của đối tượng trả về), khác với tên và mô tả của khách sạn, mà bạn có thể gán từ giá trị của đối số cho tham số HotelName. Một cái gì đó như

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

hãy xem bài viết tuyệt vời của Crockford "Khảo sát ngôn ngữ lập trình JavaScript" - http://javascript.crockford.com/survey.html Lưu ý: điều này có thể không dành cho người mới bắt đầu tuyệt đối. –

0

Có một vài thay đổi mà tôi muốn thực hiện.

Kéo chức năng HotelQuery ra khỏi chức năng ready.

Thứ hai tất cả các biến đó sẽ nằm ngoài phạm vi thời gian bạn thực hiện cuộc gọi cảnh báo. Nếu bạn muốn chúng nằm trong phạm vi, hãy khai báo chúng trên toàn cầu (bên ngoài hàm của bạn) và đặt chúng bên trong hàm.

var name; 

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

Một số vấn đề.

1) Không cần chức năng nằm trong phạm vi $(document).ready, hãy loại bỏ điều đó.


2) Mỗi ​​báo cáo trường hợp nên được theo sau bởi một break, không phải là một duy nhất ;. Ví dụ:

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

3) alert không nên được theo sau bởi một : trong onclick xử lý của bạn:

alert: (strHotelName, strHotelDesc, strHotelPrice); 

nên

alert(strHotelName, strHotelDesc, strHotelPrice); 

Ngoài ra, alert chỉ mất một tham số, vì vậy bạn cần phải phá vỡ nó:

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

3) Bạn đang giả định strHotelName, strHotelDescstrHotelPrice nằm trong phạm vi toàn cầu, mà họ thì không.


Nhìn chung, bạn có thể muốn thử một cái gì đó như thế này:

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

thức đặt/n ở bên của chuỗi của bạn trong một hộp cảnh báo sẽ cho phép bạn hiển thị nhiều vars với dòng tiện lợi phá vỡ trong một hộp cảnh báo.

myVar1= Data 
myVar2= more Data 
+0

/n hoặc ý bạn là \ n? – curtisk

Các vấn đề liên quan