简体   繁体   English

更改 <select>值,给定数字并从数组中获取值

[英]Change <select> values, given a number and taking values from an array

I hope that somebody can help me and I want to thank you all in advance for your help. 希望有人能帮助我,在此先感谢大家的帮助。 I have a php code that collects informations in one array made in this way: 我有一个php代码,它以这种方式收集一个数组中的信息:

$array_pages[$index][0] : menu id (it tells me in which menu I can find the page) $array_pages[$index][0] :菜单ID(告诉我可以在哪个菜单中找到页面)

$array_pages[$index][1] : here I store the page name (without extension) $array_pages[$index][1] :我在这里存储页面名称(不带扩展名)

$array_pages[$index][2] : this field contains a string (with more informations about the page) $array_pages[$index][2] :此字段包含一个字符串(包含有关页面的更多信息)

This multidimensional array is already built by my code. 这个多维数组已经由我的代码构建。

Here's what I would like to do: At the end of a page (where I have $array_pages ) I would like to put two select menus: 这是我想做的事情:在页面的末尾(我有$array_pages ),我想放置两个选择菜单:

<select name='themenu' style='width: 150px'>
<option value='1'> Menu number 1 </option>
<option value='2'> Menu number 2 </option>
<option value='3'> Menu number 3 </option>
</select>

In this select I would like to make the user choose a menu among the 3. The part where I am lost is the following: 在此选择中,我想让用户从3中选择一个菜单。我迷路的部分如下:

I would like to add another next to the first one. 我想在第一个旁边添加另一个。 This second select has to change its content according to the value of the first. 此第二选择必须根据第一选择的值更改其内容。 Example: 例:

If I choose Menu number 2 (value 2), the second select should display something like this: 如果我选择2号菜单(值2),则第二个选择应显示如下内容:

<select name='thepages' style='width: 150px'>
<option value='$array_pages[$index][2]'> $array_pages[$index][1] </option> 
<!-- ... -->
</select>

for each element in the array that has $array_pages[$index][0] = 2 (2 because it is the value of the first select). 对于数组中具有$array_pages[$index][0] = 2每个元素(之所以为2,是因为它是第一次选择的值)。 Is it possible to do it without refreshing the page? 是否可以在不刷新页面的情况下执行此操作?

I have tried to understand how to do something like this with javascript but I am lost and I ended up with nothing 我试图了解如何使用javascript做类似的事情,但我迷路了,最终一无所获

I hope that I have explained well enough my problem... Please help! 我希望我已经很好地解释了我的问题...请帮助! Thank you again! 再次感谢你!

The challenging part here is the interplay between JavaScript and PHP. 这里最具挑战性的部分是JavaScript和PHP之间的相互作用。 Once the page is rendered and sent to the browser, the PHP code is finished. 呈现页面并将其发送到浏览器后,PHP代码完成。 Everything else must be accomplished in JavaScript (unless you are doing some kind of post-back or AJAX model). 其他所有事情都必须用JavaScript完成(除非您正在做某种回发或AJAX模型)。

So you must make all the values completely available to the JavaScript code before the PHP is finished. 因此,必须在PHP完成之前使所有值完全可用于JavaScript代码。 Two ways you can accomplish this: 您可以通过两种方式完成此任务:

  1. Create all possible second <select> with PHP, but hide all of them ( style=display:none ). 用PHP创建所有可能的第二个<select> ,但是将所有隐藏( style=display:none )。 Then the JavaScript would show/hide the appropriate second menu depending on the selection in the first. 然后,JavaScript将根据第一个菜单中的选择显示/隐藏适当的第二个菜单。
  2. Create a JavaScript array from the PHP array that JavaScript can use to populate the second submenu dynamically when the first menu option is selected. 从PHP数组创建一个JavaScript数组,当选择了第一个菜单选项时,JavaScript可使用该PHP数组动态填充第二个子菜单。

The first option is probably simpler, at the expense of including extra HTML bloat in the response (shouldn't be a big deal if you only have a couple submenus). 第一个选项可能会更简单,但要在响应中包括额外的HTML膨胀(如果您只有几个子菜单,这没什么大不了的)。 So you would have something like this following: 因此,您将获得以下类似信息:

<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>

And your changesubmenu() JavaScript function would look something like this: 您的changesubmenu() JavaScript函数将如下所示:

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;
}​

Demo: http://jsfiddle.net/NmKvK/ 演示: http//jsfiddle.net/NmKvK/

In my experience the best way to do so with javascript is to load all the possibilities onto the page showing only the ones you select (using javascript). 以我的经验,使用javascript的最佳方法是将所有可能性加载到仅显示您选择的可能性的页面上(使用javascript)。

for example 例如

select 1
select 2 hidden
select 3 hideen

in select 1 you chose option 1 在选择1中,您选择了选项1

select 1
select 2 showing
select 3 hidden

in select 1 you chose option 1 在选择1中,您选择了选项1

select 1
select 2 hidden
select 3 showing

If you have a LOT of content then you need to do this dynamically using ajax and a second php page (or you can use the same php file). 如果您有很多内容,则需要使用ajax和第二个php页面来动态地执行此操作(或者您可以使用相同的php文件)。

So when selecting option 1 it triggers an AJAX call (with a param that is option 1 value) that loads the content of, for example, content.php into a javascript variable and from there you can load it into a place holder after the first select. 因此,当选择选项1时,它会触发AJAX调用(参数为选项1的值),例如,将content.php的内容加载到javascript变量中,然后您可以在第一个之后将其加载到占位符中选择。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM