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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化