简体   繁体   English

HTML & PHP - 获取下拉列表中选定值的索引

[英]HTML & PHP - get index of selected value in dropdown list

I have a select dropdown list just like this:我有一个像这样的选择下拉列表:

<select name="type" id="type">
<option>First option</option>

And so on...and I have more than 100 of these options.等等……我有超过 100 个这样的选项。 How can I, through PHP, retrieve the INDEX of the selected value, and not the selected value itself (For example is someone selects "First Option" I'd like to get the value "1" (or 0), instead of "First Option). I know I could just add a "value=1" but it would take a lot of time to add this for all the elements so I'd like to know if there is a quicker, easier method.我如何通过 PHP 检索所选值的 INDEX,而不是所选值本身(例如,有人选择“第一个选项”我想获得值“1”(或 0),而不是“第一个选项)。我知道我可以只添加一个“value=1”,但是为所有元素添加这个需要很多时间,所以我想知道是否有更快、更简单的方法。

Thanks a lot!非常感谢!

You could iterate through the values and count, what you get ... i do not think, this is a good way.你可以遍历值和计数,你得到什么......我不认为,这是一个好方法。 I do not think, there is a quicker, easier method to do this but using the "value" attributes.我不认为有一种更快、更简单的方法可以做到这一点,但使用“值”属性。

<option value="1">First option</option>
<option value="2">Second option</option>
<option value="3">Third option</option>
...

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

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