dqyitt2954 2015-07-02 13:36
浏览 40

下拉列表增强缺失值

I have a dropdown list that populates from the database

<td>    
   <select style="width:100%"class="CounterParty" >
   <?php

      echo '<option selected="selected"></option>';
      $stmt = $conn->prepare("SELECT DISTINCT(CounterParty)
                             FROM dbo.tbl_hist_transactions
                             ORDER BY CounterParty DESC");
      $stmt->execute();
             while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                      echo '<option >'.$row['CounterParty'].'</option>';
             }
    ?>
   </select> 
</td>

How can I modify it so that if I have a new Counterparty field that is not present in the field, the user can simply type in the new value. And it is entered in the database.

Thank You.

  • 写回答

2条回答 默认 最新

  • douqi1625 2015-07-02 13:58
    关注

    Lets assume that this is your example dropdown-list:

    <select class="CounterParty">
        <option value="ABC">ABC</option>
        <option value="XYZ">XYZ</option>
        <option value="new">New item</option>
    </select>
    

    If you use a JavaScript library like jQuery, you can add a new item to this dropdown-list dynamically when you select the pseudo-option "New Item" and enter the name of a new one. Here is the example code of it:

    $('.CounterParty').change( function(){
        var value = $(this).val();
        if (value == 'new') {
            var newItem = prompt('Add new item:');
    
            if(!newItem) return false;
    
            $(this).append('<option value"' + newItem + '" selected="selected">' + newItem + '</option>');
        }
    });
    

    After the new item was added and the form has been submitted, the value can be stored in the database like any other value too.

    Here is the example fiddle: http://jsfiddle.net/Lms1p5a3/

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看