JQuery:如何从下拉列表中选择不区分大小写的值? [英] JQuery: How do I select a value from a dropdownlist case insensitive?

查看:133
本文介绍了JQuery:如何从下拉列表中选择不区分大小写的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的方案:


  • 我有一个用户输入文本的文本框

  • 文本框有一个onblur函数,尝试从下拉列表中选择一个值(如果存在),基于文本框输入

这个工作完全正常如果textbox值与dropdownlist值相同。但是我希望它不区分大小写。

This works perfectly fine if the textbox value is the same case as the dropdownlist value. But I want it to be case insensitive.

所以如果用户键入stackoverflow,那么我希望它从下拉列表StackOverflow中进行选择。如何通过jQuery执行此操作?

So if the user types "stackoverflow" then I want it to select from the dropdownlist "StackOverflow". How can I do this via jQuery?

推荐答案

这是另一种方法:在实际的情况下找到匹配值StackOverflow并调用 val()

Here's another approach: find the matching value in its actual case, "StackOverflow," and call val() with that.

  var matchingValue = $('#select option').filter(function () { 
      return this.value.toLowerCase() == 'stackoverflow'; 
  } ).attr('value');    
  $('#select').val(matchingValue);

当然,文字'stackoverflow'应该用变量替换。

Of course, the literal 'stackoverflow' should be replaced with a variable.

这篇关于JQuery:如何从下拉列表中选择不区分大小写的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆