[英]KnockoutJS - Select next option in dropdown select box
I have a dropdown list of sellers. 我有一个卖家下拉列表。 Currently, the first seller is selected.
当前,选择了第一个卖方。 When a button is clicked, it does some processing, then needs to automatically select the next seller in the list.
单击按钮后,它会进行一些处理,然后需要自动选择列表中的下一个卖方。 I found jQuery code that does this, but it is not updating the Knockout value.
我找到了执行此操作的jQuery代码,但它没有更新Knockout值。 I think that is because I am not using Knockout to change the selected value.
我认为这是因为我没有使用Knockout更改所选值。
HTML: HTML:
<select data-bind="value: sellerID, options: $root.sellers, optionsValue: 'Value', optionsText: 'Text', optionsCaption: ' -- select a student --'" class="form-control" id="sellerSelect"></select>
ViewModel: 视图模型:
var SellerViewModel = function(groups) {
var self = this;
self.json = groups;
self.groups = ko.computed(function() {
var opts = [];
for(var key in self.json)
{
if(self.json.hasOwnProperty(key))
{
opts.push({Text: self.json[key].majorGroup + " / " + self.json[key].minorGroup, Value: self.json[key].groupId});
}
}
return opts;
});
self.roomID = ko.observable();
self.roomID.subscribe(function(group) {
if(group) {
$.ajax({
type: "GET",
url: '@Url.Action( "GetSellers", "Tally" )',
data: { groupId: group, contractId: $("#contractId").val() },
success: function (data) {
var opts = [];
for (var key in data.sellers) {
if (data.sellers.hasOwnProperty(key)) {
opts.push({Text: data.sellers[key], Value: key});
}
}
self.sellers(opts);
$("#sellerGroupId").val(group);
}
});
}
});
self.sellers = ko.observableArray([]);
self.hasSellers = ko.observable(false);
self.sellerID = ko.observable();
self.sellerID.subscribe(function(seller) {
if(seller && verify == "True") {
getVerifySeller(seller);
easyGlanceModel.bigItemNo(null);
easyGlanceModel.bigQty(null);
tallyViewModel.highlightedRowIndex(null);
}
});
self.phone = ko.observable();
self.prize = ko.observable();
self.firstName = ko.observable();
self.lastName = ko.observable();
}
And this is the jquery I found to select the next option in a dropdown: 这是我在下拉菜单中选择下一个选项的jQuery:
$('#sellerSelect option:selected').next().attr('selected', 'selected');
But I think that last line of JQuery needs to be Knockout instead. 但是我认为JQuery的最后一行需要改为Knockout。 But I don't know how or if is possible to do that in Knockout.
但是我不知道在淘汰赛中如何或是否有可能做到这一点。 Either way, I need the sellerID to be updated with the new seller's ID.
无论哪种方式,我都需要使用新的卖方ID更新卖方ID。 Currently, it is always the first seller's ID.
当前,它始终是第一个卖家的ID。
Update: Here is the returned JSON from GetSellers: 更新:这是GetSellers返回的JSON:
{"sellers":{"1492":"MORGAN R","1493":"LYDIA P","1494":"MADISON G","1495":"TREYTON T","1496":"ZACH D","1497":"CAMERON P","1498":"REGAN R","1499":"EVELYN B"}}
can't you just set the selected item to the next one in the array on the click event. 您不能只是将所选事件设置为click事件上数组中的下一项。 I put it in as an attribute but I'm sure you can get the next one in many ways.
我将其作为属性输入,但是我敢肯定,您可以通过多种方式获得下一个属性。 run the snippet below.
运行下面的代码段。 select a country click process.
选择国家/地区点击过程。 it will process that country and set the select to the next country.
它将处理该国家/地区并将选择设置为下一个国家/地区。
var Country = function(name, population, nextindex) { this.countryName = name; this.countryPopulation = population; this.nextindex = nextindex }; function viewModel() { var self = this; this.availableCountries = ko.observableArray([ new Country("UK", 65000000, 1), new Country("USA", 320000000, 2), new Country("Sweden", 29000000, 0) ]); this.selectedCountry = ko.observable(''); this.click = function(value) { alert('you just processed ' + value.countryName) self.selectedCountry(self.availableCountries()[value.nextindex]); } } ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <p> Your country: <select data-bind="options: availableCountries, optionsText: 'countryName', value: selectedCountry, optionsCaption: 'Choose...'"></select> </p> <div data-bind="with: selectedCountry"> <!-- Appears when you select something --> You are processing <span data-bind="text: countryName"></span>. <button data-bind="click: $parent.click">Process</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.