使用jQuery基于文本框输入在下拉列表中选择选项

时间:2014-04-16 15:48:02

标签: jquery

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

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

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并选择[title =" RYTest"]是相同的元素。

Thansk!

1 个答案:

答案 0 :(得分:0)

这样做。

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