问题:
当输入两个表单值(最低/最高价格)时,不会更新呈现的查询结果。首次输入时,结果会正确显示,但随后使用新参数更改表单值时,html 代码不会更新以反映这些参数。
HTML 代码:
<input type="text" size="5" placeholder="Min. price" id="price_min">
<input type="text" size="5" placeholder="Max. price" id="price_max">
<input type="button" onclick="getHotelInfo()" value="Get" />
<div id="hotel_info"></div>
JavaScript 代码:
function getHotelInfo() {
$.get('hotelservice.php?priceMin=' + $('input#price_min').val() + '&priceMax=' + $('input#price_max').val(), function(data) {
var hotelInfo = JSON.parse(data);
content = '<table style="font-size:10px;width:100%;"><tr><th>Name</th><th>Stars</th><th>Price</th></tr>';
for (var hotel=0; hotel<5;hotel++)
{
content += '<tr><td><a href="' + hotelInfo[hotel].externalLink + '">' + hotelInfo[hotel].name +'</a></td><td><center>' + hotelInfo[hotel].stars + '</center></td><td><center>' + hotelInfo[hotel].price + '</center></td></tr>';
}
content += '</table>';
$('#hotel_info').replaceWith(content);
});
}
PHP代码:
$priceMin = $_GET['priceMin'];
$priceMax = $_GET['priceMax'];
$xml_source = file_get_contents('http://www.kayak.com/h/rss/hotelrss/SE/vaxjo?mc=EUR');
$xml = simplexml_load_string($xml_source);
$result = array();
foreach ($xml->channel->item as $item) {
$kyk = $item->children('http://www.kayak.com/h/rss/hotelextension');
$price = (int)$kyk->price;
if ($price < $priceMax && $price > $priceMin) {
$entry = new stdClass();
$entry->name = (string)$item->title;
$entry->externalLink = (string)$item->link;
$entry->price = $price;
$entry->stars = (int)$kyk->stars;
$result[] = $entry;
}
}
echo json_encode($result);
期望的输出: 当表单值发生变化并调用 getHotelInfo() 时,应该反映新的结果。