duanjiaolia97750
2012-01-18 10:36 阅读 57
已采纳

optgroup获取与php标签关联

I have following code in a html form

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzhunnai0140 dongzhunnai0140 2012-01-18 10:47

    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.

    点赞 评论 复制链接分享
  • dongsuoying9059 dongsuoying9059 2012-01-18 10:48

    you could make the values arrays e.g.

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

    so then your $_POST['category'] should return an array

    点赞 评论 复制链接分享

相关推荐