gpt4 book ai didi

javascript - 使用 knockout 创建的 jquery 选择选项

转载 作者:行者123 更新时间:2023-12-02 14:10:39 25 4
gpt4 key购买 nike

我正在尝试从 select 中选择一个选项,我认为该选项是使用 knockoutjs 创建的(但我不确定)

<select class="styled" data-bind="options: Locations, optionsText: 'Name', value: DesiredLocat…ion: SelectLocationText(), event: { change: UpdateLocation }">

<option value="">

Select location

</option>
<option value="">

Some location

</option>
<option value="">

Some location2

</option>

</select>

整个东西都捆绑在一个框架中,我已经在谷歌上搜索了如何使jquery在框架中工作,现在我像这样使用它

var frameDocument= $("frame[name='mainFrame']", top.document)[0].contentDocument;

我尝试这样做:

$(frameDocument).find('.styled').val('Some location').change();

像这样:

$(frameDocument).find('.styled').children().each(function(){
if ($(this).text()=='Some location'){
$(this).click();
return false;
}
})

任何人都可以帮助我如何选择我需要的选项吗?

最佳答案

最好不要使用 jquery,因为您使用的是 knokout.js

示例:https://jsfiddle.net/kyr6w2x3/82/

HTML:

<select class="styled" data-bind="options: Locations, optionsText: 'Name', optionsValue: 'Id',value: selectedLocation">

JS:

function MainViewModel() {
var self = this ;
self.Locations = ko.observableArray([{Name:"Location 1" , Id : 1 },{Name:"Location 2" , Id : 2 },{Name:"Location 3" , Id : 3 }]);
self.selectedLocation = ko.observable();
self.selectedLocation.subscribe(function(newValue){
console.log(newValue);
})
}

ko.applyBindings(new MainViewModel());

关于javascript - 使用 knockout 创建的 jquery 选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39623575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com