【问题标题】:Select option in drop down list based on text box input using jQuery使用jQuery根据文本框输入在下拉列表中选择选项
【发布时间】:2014-04-16 15:48:02
【问题描述】:

根据我的选择,我有几个下拉列表,以及一些必要的计算,我希望能够自动选择另一个下拉列表的值。我之前配置它的方式是使用文本框而不是下拉列表,但我的项目要求需要我切换到下拉列表。

这是我使用文本框时的方式:

var minusOne =        ["19","23","201","202","205","206","215","216","217","224","225","226","228","238","237","245","246","247","248","249","250","251","73","207","208","209","210","211","212","213","214","203","227","309","1302","134","135","136","142","762"];
var plusOne =["37"];
$("select[title='Crop Year']").change(function() {
var year = Number($(this).val());
var crop = $("select[title='Crop'] option:selected").text().split(' -');
var index = $.inArray(crop[0],minusOne),
indexII = $.inArray(crop[0],plusOne);
//TEXT BOX
index >= 0 ? $("input[title='RY']").val(year-1) : indexII >= 0 ?      $("input[title='RY']").val(year+1) : $("input[title='RY']").val(year);});

现在我想将 //Text Box 下面的部分更改为下拉列表。这是我到目前为止没有运气的尝试:

........
//DROP DOWN - Option I  
if (index >= 0){$('select[title="RYTest"]').find(year-1).attr('selected','selected');}
else if (indexII >= 0)  {$('select[title="RYTest"]').find(year+1).attr('selected','selected');}
else {$('select[title="RYTest"]').find(year).attr('selected','selected');}});

//DROP DOWN - Option II

if (index >= 0) {$('#LookupID  option[text="' + year-1 + '"]').prop('selected',true);}
else if (indexII >= 0){$('#LookupID option[text="' + year+1 + '"]').prop('selected',true);}
else {$('#LookupID option[text="' + year + '"]').prop('selected',true);}});

我真的很感激这里的任何帮助。我只是想让您知道,我无法在此列表中添加或删除选项(没有 .prepend($('') 等)。此下拉列表从 SharePoint 列表中获取信息。另请注意#LookupID 和 select[title="RYTest"] 是同一个元素。

谢谢!

【问题讨论】:

    标签: jquery


    【解决方案1】:

    这样就可以了。

    $("select[title='RYTest'] option:contains(" + $("input[title='RY']").val() + ")").attr('selected', 'selected');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 2023-03-04
      • 2014-05-24
      • 2019-09-06
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多