简体   繁体   English

根据jQuery中的文本框值更改下拉选择的值

[英]change dropdown selected value based on textbox value in jquery

I have a textbox and selected option list of 4 options like below... 我有一个文本框和4个选项的所选选项列表,如下所示...

<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 选择的主要价值来自php和基于用户输入的ajax,所以我只想在上面的选项列表中设置选择的价值

below is my jquery code 下面是我的jQuery代码

$(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 我的jQuery代码工作正常,它设置了选定的选项,但没有显示选定的选项,它使用了inspect

 $(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"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM