drf21989 2015-04-28 07:27
浏览 34
已采纳

根据输入jquery中键入文本的第一个字母自动选择下拉列表

Hello I tried to search something equivalent without success, this is not a php based value where one drop down updates another. its a simpler version but I don't know how to proceed

Basically I have a text field like

 <input size="50" name="productname" id="product" type="text" />

 <select name="platform">
    <?php if($manufacturers) foreach($manufacturers as $each) { ?>
           <option value="<?=$each['id']?>"><?=$each[ 'manufacturerName']?></option>
    <?php } ?>
 </select>

I noticed that in about 90% of my scenario if a manufacturer name is Samsung most of its product will start with S that is the first name of the manufacturer.

In my select I have some manufacturers for example , Samsung, Alcatel, Hitachi, from a database.

In this form I am doing user will be adding new product, for example Samsung S6, as he types Samsung, I wish a jquery will set the selected option to the select list to samsung most probably based on the first matching letter.

  • 写回答

2条回答 默认 最新

  • doutidi5037 2015-04-28 08:06
    关注

    Try this code, then you can modified with your own creativity.

    $('#product').on('keyup', function () {
       var input_val = String($(this).val());
       var sel = $('select[name=platform]');
    
       if (input_val !== "") {
          var h = $('select option[value*=' + input_val + ']');
          if (h.length > 0) {
              h.prop('selected', true);
          } else {
              sel.val("");
          }
       } else {
          sel.val("");
       }
    
    });
    

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!