PHP 获取下拉列表选择选项值

2021-12-28 00:00:00 drop-down-menu php

在我的下拉列表中,每个选项都有两个不同的值.我怎样才能检索两者?让我来说明一下我的意思.

In my dropdownlist I have two different values for each option. How can I retrieve both? Let me illustrate what I mean.

<select name="my_ddl">
  <option value="<?php $value_Id ?>"><?php $value_text ?></option>
  <option value="<?php $value_Id ?>"><?php $value_text ?></option>
</select>

当表单发布时,我希望能够同时获取所选选项的 $value_id 和 $value_text.我该怎么做?

When the form is posted, I want to be able to get both the $value_id and $value_text of the selected option. How can I do this?

$_POST['my_ddl'] 只能得到一个值,不能同时得到两个值.

$_POST['my_ddl'] only gets one value not both.

在 asp.net 中,我可以通过引用 my_ddl.Value 和 my_ddl.Text 来做到这一点.

In asp.net I could do this simply by referring to my_ddl.Value and my_ddl.Text.

谢谢!

推荐答案

严格来说,这是不可能的.

Strictly, this is not possible.

你可以做的是在你的value属性中使用一个分隔符:

What you could do is use a delimiter in your value attribute:

<select name="my_ddl">
  <option value="<?php echo $value_Id ?>|<?php echo $value_text ?>"><?php echo $value_text ?>   
  </option>
</select>

还有……

<?php    
   list($id, $text) = explode('|', $_POST['my_ddl']);
   //...
?>

相关文章