douxianxing5712 2016-10-20 09:07
浏览 110

根据jquery中的文本框值更改下拉列表选定值

I have a textbox and selected option list of 4 options like below...

<select id="select2" class="form-control">
                <option value="pre">Pre</option>
                <option value="rep" selected="selected">Rep</option>
                <option value="new">New</option>
                <option value="dec">Dec</option>
              </select>

and a textbox where user input some keywords and based on keywords i want to change selected option in above option list

<td id="fpv_col1"  contenteditable="">decorating and repair work</td>

main value for selction comes from php and ajax based on user input so i just want to set selected value in above option list

below is my jquery code

$(document).on('keyup', '#fpv_col1', function(event) {
    var desc_text = $(this).text();
    $.ajax({
            url: 'php/fpv_lbyl_prediction.php',
            type: 'POST',
            data:{
                    value1:desc_text,

            },
            dataType:'text',
            success: function(data){
                $("#error_msg").fadeIn();
                $("#error_msg").html("<strong>Success!</strong> "+data);
                //$('select#select2 option').removeAttr('selected');
                //$('select#select2').find('option[value='+data+']').attr("selected",true);

                $('select#select2 option').removeAttr('selected').filter('[value='+data+']').attr('selected', true);  
            }
        });

event.preventDefault();

});

my jquery code works fine, it set selected option but not displaying selected option, it saw using inspect

  • 写回答

1条回答 默认 最新

  • dpka7974 2017-01-03 07:15
    关注

    $(document).ready(function() {
      $('#otherCategory').keyup(function() {
        if ($(this).val() == 0) {
          $('#category').val(1);
        } else if ($(this).val() > 0) {
          $('#category').val(2);
        } else {
          $('#category').val(0);
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <select id="category" name="category">
      <option value="0">Please select</option>
      <option value="1">Clear</option>
      <option value="2">UnClear</option>
    </select>
    
    <input type="text" id="otherCategory" name="otherCategory">

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘