您的代码不起作用,因为变量的作用域是函数HotelQuery
.我认为您可能想要做的是返回一个具有该函数属性的对象,并使用不显眼的JavaScript方法将click事件处理程序绑定到该元素.
就像是
$(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)没有必要将函数放在内部$(document).ready
,摆脱它.
2)每个案例陈述应该后跟一个break
,而不是一个;
.例如:
function HotelQuery(HotelName) { switch (HotelName) { case 'TetrisHotel': // stuff goes here ... break; //end Tetris Hotel }; }
3)你的处理程序中alert
不应该跟a ::
onclick
alert: (strHotelName, strHotelDesc, strHotelPrice);
应该
alert(strHotelName, strHotelDesc, strHotelPrice);
此外,alert
只需要一个参数,因此您需要将其分解:
alert(strHotelName); alert(strHotelDesc); alert(strHotelPrice);
3)你在假设strHotelName
,strHotelDesc
并且strHotelPrice
在全球范围内,他们不是.
总而言之,您可能想尝试这样的事情:
hotel query Tetris Hotel Query