我试图这样做,当一个人从下拉菜单中选择一个选项时,会出现另一个下拉菜单。所以,如果我有一个有3个选项的下拉菜单,“温哥华”,“新加坡”,“纽约”。当用户选择温哥华时,下拉菜单显示有几个选项,如果他们选择纽约另一个淹没菜单为了让事情变得复杂,我也在使用php,因为它最终会在服务器端运行。
echo "<script type=\"text/javascript\">";
echo "function getDropDown(sel){";
echo "hideAll();";
echo "document.getElementById(sel.options[sel.selectedIndex].value).style.display ";
echo "= 'block';";
echo "}";
echo "function hideAll(){";
echo "document.getElementById(\"vancouver\").style.display = 'none';";
echo "document.getElementById(\"singapore\").style.display = 'none';";
echo "document.getElementById(\"newyork\").style.display = 'none';";
echo "}";
echo "</script>";
echo "<tr>";
echo "<td align=\"right\">";
echo "<td>";
echo "<select name=\"optionDrop\" onChange=\"getDropDown(this)\">";
echo "<option value=\"\">Please Select</option>";
echo "<option value=\"vancouver\">Vancouver</option>";
echo "<option value=\"singapore\">Singapore</option>";
echo "<option value=\"newyork\">New York</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=\"right\">City</td>";
echo "<td>";
echo "<div id=\"vancouver\" style=\"display: none;\">";
echo "<select name=\"optionDrop\" >";
echo "<option value=\"\">Please Select</option>";
echo "</select>";
echo "</div>";
echo "<div id=\"singapore\" style=\"display: none;\">";
//echo "<select name=\"optionDrop2\">";
// echo "<option value=\"\">Please Select2</option>";
echo "Sing";
echo "</div>";
echo "<div id=\"newyork\" style=\"display: none;\">";
echo "New York";
echo "</div>";
echo "</td>";
echo "</td>";
echo "</tr>";
现在,当我选择“温哥华”选项时,下拉菜单会弹出,当选择其他选项时,会显示文本。但是,如果我取消注释行
//echo "<select name=\"optionDrop2\">"; // echo "<option value=\"\">Please Select2</option>";
然后没有任何作品了。无论我选择什么选项都没有显示出来。我完全陷入困境,无法弄清楚它有什么问题。
答案 0 :(得分:1)
在php文件中关闭php标签不会破坏你的代码。 如果您只想在您发布的代码中显示HTML,请执行以下操作:
<?php
$variable = "test";
// Some random things blabla.
?>
<span class="test">
<?php echo $variable; ?>
</span>
<?php
$something = "something_else";
// Some other php
?>
关于你的问题,你去了: http://codepen.io/anon/pen/GxytE