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

时间:2016-10-20 09:07:22

标签: javascript php jquery ajax

我有一个文本框和4个选项的选定选项列表,如下所示......

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

和一个文本框,其中用户输入一些关键字并基于我想要更改上面选项列表中选定选项的关键字

$(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();

});

选择的主要价值来自基于用户输入的php和ajax 所以我只想在上面的选项列表中设置选定的值

下面是我的jquery代码

private string GetRandomFolder(string root)
{
  var rnd = new Random();
  var path = root;
  var depth = rnd.Next(0, 7);

  for (int i = 0; i < depth; i++)
  {
    path = this.GetRandomFolder(path);
    if (path == "")
      break;
  }

  return output;
}
private string GetRandomSubFolder(string root)
{
  var di = new DirectoryInfo(root);
  var dirs = di.GetDirectories();
  var rnd = new Random();

  if (dirs.Length == 0)
    return "";

  return dirs[rnd.Next(0, dirs.Length - 1)].FullName;
}

我的jquery代码工作正常,它设置了选中但没有显示选中的选项,它看到了使用inspect

1 个答案:

答案 0 :(得分:0)

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