【发布时间】: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