duanbo5230 2015-08-03 12:04
浏览 95
已采纳

PHP - 根据从选项中选择的内容获取值

<select class="selectpicker" name="mscr_type" id="mscr_type">

            <?php $fpp_type = '';
                  foreach ($getmilestone_cr as $mscr_list) {

                  $description = $mscr_list['FIX_PRJ_DESC'];//fetch  description

                    if ($fpp_type != $mscr_list['FIX_PRJ_TYPE']) {
                        if ($fpp_type != '') {
                            echo '</optgroup>';
                    }

                    if($mscr_list['FIX_PRJ_TYPE'] == 'MS'):
                        echo $fpp_label = "Milestone";
                    elseif($mscr_list['FIX_PRJ_TYPE'] == 'CR'):
                        echo $fpp_label = "Change Request(CR)";
                    endif;

                    echo '<optgroup label="'.$fpp_label.'">';

                    }

                    echo '<option value="'.$mscr_list['FIX_PRJ_TYPE'].'">'.htmlspecialchars($mscr_list['FIX_PRJ_NAME']).'</option>';
                    $fpp_type = $mscr_list['FIX_PRJ_TYPE'];    
                }

                    if ($fpp_type != '') {
                        echo '</optgroup>';
                    }
            ?>
</select>

The above code outputs:

<select class="selectpicker">
    <optgroup label="Picnic">
      <option value ="1">Mustard</option>
      <option value ="2">Ketchup</option>
      <option value ="3">Relish</option>
    </optgroup>
    <optgroup label="Camping">
      <option value ="4">Tent</option>
      <option value ="5">Flashlight</option>
      <option value ="6">Toilet Paper</option>
    </optgroup>
  </select>

How could I fetch description based on what is selected from selected option. For example if Mustard is selected, then input value should be filled with value 'Mustard is yellow in color'.

Tried using <select class="selectpicker" name="mscr_type" id="mscr_type" onchange="document.getElementById('mscr_description').value=this.options[this.selectedIndex].text"> but it fetches the name and not description.

<label for="name" class="label">Description</label>  
<input type="text" disabled="" id="selectpicker_description" name="selectpicker_description" class="span3">
  • 写回答

1条回答 默认 最新

  • dongyi1921 2015-08-03 12:12
    关注

    Add the description to the HTML using a data-XXX attribute:

    echo '<option value="'.$mscr_list['FIX_PRJ_TYPE'].'" data-description="'.htmlspecialchars($description).'">'.htmlspecialchars($mscr_list['FIX_PRJ_NAME']).'</option>';
    

    Then you can use

    onchange="updateDescription(this)"
    

    with the Javascript function:

    function updateDescription(sel) {
        var output = document.getElementById('mscr_description');
        var desc = sel.options[sel.selectedIndex].getAttribute('data-description');
        output.value = desc;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教