Php 改变<;选择>;值,给定一个数字并从数组中获取值
我希望有人能帮助我,我想提前感谢你们的帮助。 我有一个php代码,它以这种方式在一个数组中收集信息:Php 改变<;选择>;值,给定一个数字并从数组中获取值,php,javascript,select,Php,Javascript,Select,我希望有人能帮助我,我想提前感谢你们的帮助。 我有一个php代码,它以这种方式在一个数组中收集信息: $array\u pages[$index][0]:菜单id(它告诉我可以在哪个菜单中找到页面) $array\u pages[$index][1]:我在这里存储页面名称(无扩展名) $array\u pages[$index][2]:此字段包含一个字符串(包含有关页面的更多信息) 我的代码已经构建了这个多维数组 以下是我想做的: 在页面的末尾(我有$array\u pages),我想放置两个选
$array\u pages[$index][0]
:菜单id(它告诉我可以在哪个菜单中找到页面)
$array\u pages[$index][1]
:我在这里存储页面名称(无扩展名)
$array\u pages[$index][2]
:此字段包含一个字符串(包含有关页面的更多信息)
我的代码已经构建了这个多维数组
以下是我想做的:
在页面的末尾(我有$array\u pages
),我想放置两个选择菜单:
1号菜单
2号菜单
3号菜单
在此选择中,我想让用户在3个菜单中选择一个菜单。我迷路的地方如下:
<select name='themenu' style='width: 150px' onchange='changesubmenu(this)'>
<option value='1'> Menu number 1 </option>
<option value='2'> Menu number 2 </option>
<option value='3'> Menu number 3 </option>
</select>
<select id="thepages1" style="display:none">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
<select id="thepages2" style="display:none">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
<select id="thepages3" style="display:none">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
我想在第一个旁边再加一个。第二个选择必须根据第一个选择的值更改其内容。例如:
如果选择菜单编号2(值2),则第二次选择应显示如下内容:
var tempid=''; // save previous id to hide it
function changesubmenu(parent) {
// get selected menu id
var id = parent.value;
var submenuid = "thepages"+id;
// show the selected menu
var submenuel = document.getElementById(submenuid);
if (submenuel) submenuel.style.display = "";
// hide the previously selected menu
var oldsubmenuid = "thepages"+tempid;
var oldsubmenuel = document.getElementById(oldsubmenuid);
if (oldsubmenuel) oldsubmenuel.style.display = "none";
// update old id for reference
tempid = id;
}
$array_pages[$index][1]
对于数组中具有$array\u pages[$index][0]=2
(2,因为它是第一次选择的值)的每个元素。是否可以在不刷新页面的情况下执行此操作
我试图理解如何用javascript做类似的事情,但我迷路了,结果一无所获
我希望我已经很好地解释了我的问题。。。请帮忙!再次感谢你 这里最具挑战性的部分是JavaScript和PHP之间的相互作用。一旦页面呈现并发送到浏览器,PHP代码就完成了。其他一切都必须在JavaScript中完成(除非您正在执行某种回发或AJAX模型) 因此,在PHP完成之前,必须使所有值完全可用于JavaScript代码。有两种方法可以实现这一点:
,但隐藏所有这些(style=display:none
)。然后JavaScript将根据第一个菜单中的选择显示/隐藏相应的第二个菜单<select name='themenu' style='width: 150px' onchange='changesubmenu(this)'>
<option value='1'> Menu number 1 </option>
<option value='2'> Menu number 2 </option>
<option value='3'> Menu number 3 </option>
</select>
<select id="thepages1" style="display:none">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
<select id="thepages2" style="display:none">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
<select id="thepages3" style="display:none">
<option>...</option>
<option>...</option>
<option>...</option>
</select>
演示:根据我的经验,使用javascript最好的方法是将所有可能性加载到页面上,只显示您选择的可能性(使用javascript) 比如说
select 1
select 2 hidden
select 3 hideen
在“选择1”中,您选择了选项1
select 1
select 2 showing
select 3 hidden
select 1
select 2 hidden
select 3 showing
在“选择1”中,您选择了选项1
select 1
select 2 showing
select 3 hidden
select 1
select 2 hidden
select 3 showing
如果您有很多内容,那么您需要使用ajax和第二个php页面(或者您可以使用相同的php文件)动态地执行此操作
因此,当选择选项1时,它会触发一个AJAX调用(参数为选项1的值),该调用将content.php的内容加载到一个javascript变量中,在第一次选择之后,您可以从该变量将其加载到占位符中
希望这有帮助 铁人不能这样做吗?:)首先,对于初学者来说,您需要使用json\u encode()
将足够的数据从$array\u页面
传递到JS中。这是一个以前解决过很多次的问题。基本上,您需要一个javascript事件监听器,当第一次选择被更改时,该监听器将被调用。然后,它将对php脚本执行ajax调用,返回第二次选择的数据。