我想从网络服务中提取一条记录并在div中很好地显示它,就像在this example中一样。
当有人选择“天数”时,它应该过滤结果。
这是我到目前为止所做的:
<script>
function itinerary(name)
{
var last = document.getElementById(selected);
selected = name;
var current = document.getElementById(selected);
last.style.display = "none";
current.style.display = "";
document.getElementById("selectItinerary").blur();
}
</script>
<?php
ini_set("display_errors", 1);
$days = "http://services.gerbertours.com/tours.asmx/Days?type=2&cityID=1146";
$ch1 = curl_init($days);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); // make sure we get the response back
$xml1 = curl_exec($ch1); // execute the post
curl_close($ch1); // close our session
$var1 = simplexml_load_string($xml1);
$configData1 = json_decode($var1, true);
$days = 0;
foreach ($configData1 as $key=>$val) {
if ($val > $days) {
$days = $val;
}
}
//echo $days;
//$days = "http://services.gerbertours.com/tours.asmx/Days?type=2&cityID=1146";
$url = "http://services.gerbertours.com/tours.asmx/Sample?type=2&CityID=1146&days=$days";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // make sure we get the response back
$xml = curl_exec($ch); // execute the post
curl_close($ch); // close our session
$var = simplexml_load_string($xml);
$configData = json_decode($var, true);
echo '<pre>';
print_r($configData);
echo '</pre>';
foreach ($configData as $row) {
echo 'Day:' . $row['Day'].'<br>';
echo 'Time:' .$row['Time'].'<br>';
echo 'Description:' .$row['Description'].'<br>';
}
?>
<select id="selectItinerary" onchange="itinerary(this.options[this.selectedIndex].value); this.blur" style="font-size:11px">
<?php
for ($day = 1; $day <= $days; $day++) {
?>
<option value="<?php echo $day; ?>"><?php echo $day; ?> Day Tour</option>
<?php
}
?>
</select>
<?php
$dayCount = 1;
foreach ($configData as $row) {
$currentDay = $row['Day'];
if($dayCount==$days);break;
?>
<div id="<?php echo $dayCount; ?>" class="itinerary">
<div class="day">
<?php echo 'Day:' . $row['Day']; ?>
</div>
<div class="time">
<?php echo 'Time:' .$row['Time']; ?>
</div>
<div class="description">
<?php echo 'Description:' .$row['Description']; ?>
</div>
</div>
<?php
$dayCount++;
}
?>
其他一切都没问题。我需要做的就是根据用户选择过滤结果,如果有人选择“3日游”,它应该显示结果中的3个项目。我怎样才能做到这一点?
答案 0 :(得分:0)
如果您不使用Jquery,则可以使用以下方式显示/隐藏元素:
document.getElementById('elementId').style.display = 'block'; // to show
document.getElementById('elementId').style.display = 'none';
想法是隐藏所有元素,然后当onchange事件触发时,显示要显示的元素。因此,在您的代码中,您只需要更改
current.style.display = "";
到
current.style.display = "block";
我之前也把它们放在一起,所以你在显示所需的div之前隐藏了你需要隐藏的所有内容:
document.getElementById('1').style.display = 'none';
document.getElementById('2').style.display = 'none';
document.getElementById('3').style.display = 'none';
但是,当然,对于大型项目,我建议使用Jquery或任何Javascript框架来简化操作。查看:Jquery dynamic hide and show for drop down menu