简体   繁体   English

optgroup获取关联的php标签

[英]optgroup get in php label associated

I have following code in a html form 我有以下HTML格式的代码

<select name="category" class="input" onchange="ShowTB(this,'suggest');">
      <option value="0" selected="selected">
        [choose yours]
      </option>
      <optgroup label="Item">
      <option value="SubItem1"SubItem1</option>
    <option value="SubItem2">SubItem2</option>
  </optgroup>
      <option value="Item2">Item2</option>
      <optgroup label="Item3">
     <option value="SubItem4"SubItem4</option>
     <option value="SubItem5">SubItem5</option>
  </optgroup>
      <option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>

</select>

in php i get the value of field selected with: 在PHP中,我得到选择的领域的价值:

$category = $_POST['category'];

in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3 How to ? 在这种模式下,如果我选择以下形式:SubItem1,在php中我得到了值SubItem1但我也想获得关联的标签,即:Item;或者如果我选择SubItem5我也得到了SubItem5但我也想得到关联的标签,即:Item3如何?

Indeed, you only get the value. 确实,您只会获得价值。 If you need more, just encode whatever you want into the value, for example: 如果需要更多,只需将所需的值编码为值,例如:

<option value="Item3.SubItem5">SubItem5</option>

Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label. 或者,您可以使用javascript来捕获select字段上的onChange事件,并使用所需标签更新隐藏的输入字段。

you could make the values arrays eg 您可以使值数组例如

<option value="Item3[SubItem5]">SubItem5</option> 

so then your $_POST['category'] should return an array 因此,您的$ _POST ['category']应该返回一个数组

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

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