2014-04-16 90 views
1

我有几个下拉列表,根据我的选择和一些必要的计算,我希望能够自动选择值另一个下拉列表名单。我之前配置过的方式是使用文本框而不是下拉菜单,但是我的项目需求需要我切换到下拉列表。基于文本框输入使用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);}); 

现在我想//文本框下方的部分更改为下拉列表中。这是我迄今为止没有运气的尝试:

........ 
//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并选择[标题= “RYTest”]是相同的元素。

Thansk!

回答

0

这会做到这一点。

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