通过搜索/筛选部分选项文本并将值作为jQuery变量输出,从选择选项获取值

通过搜索/筛选部分选项文本并将值作为jQuery变量输出,从选择选项获取值,jquery,select,select-options,Jquery,Select,Select Options,我在页面上有一个选择框,如下所示: <select id="mySelect" autocomplete="off" class="inputbox input-large"> <option value="">- Select option -</option> <option value="1">Car</option> <

我在页面上有一个选择框,如下所示:

<select id="mySelect" autocomplete="off" class="inputbox input-large">
 <option value="">- Select option -</option>
 <option value="1">Car</option>
 <option value="12">- Plane</option>
 <option value="34">- - Train</option>
</select>
<input type="text" id="myInput" name="myInput" value="Train">

对于每个愿意写下代码示例的人,请从头开始,因为上述内容可能在100种不同的方面都是错误的-

看起来你并没有走得太远:

在这里返回:

停止运行下一行中的exists=true-返回在该点退出匿名筛选函数,并且在该函数中运行后没有代码

有两个选项可以让你在不太修改代码的情况下想到

使用.filter,然后检查结果 每个和集合都存在于循环中 在第二种情况下,使用each=each | |[test]意味着一旦设置,它将保持为真-如果使用简单的for,那么一旦找到,就会添加一个中断

$str=$'myInput'.val; var=false; var filtered=$mySelect option.filterfunction{ 返回$this.text.replace//g.trim==$str; }; 存在=已过滤。长度>0; console.logexists; 存在=假; $mySelect option.eachfunction{ exists=exists | |$this.text.replace//-/g.trim==$str; }; console.logexists; -选择选项- 汽车 -飞机 --训练
非常感谢您所需要的一切!
  $str = $('#myInput').val();

  var exists = false;
  $("#mySelect option").filter(function() {
      return $(this).text().replace(/-/g,"").trim() == $str;
      var exists = true;
  });
  console.log(exists);
    return $(this).text().replace(/-/g,"").trim() == $str;
    exists = true;