2016-09-21 125 views
0

我试图选择我认为这是使用knockoutjs创建选择一个选项(但我不知道)jQuery的这是用基因敲除

<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> 

整个事情是在框架中捆绑的创建选择选项,我GOOGLE了如何使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; 
     } 
}) 

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

回答

2

由于您使用的是knokout.js,所以最好不要使用jquery

例子: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()); 
+0

我应该如何选择使用基因敲除的选项? – Asiat

+0

的事情是,我不是网站的所有者,我正在为它写一个爬虫,我需要选择一个选项。在Chrome中,当我在开发者控制台中输入'ko'时,它给了我这个对象,但是,在firefox中它没有。我相信它与框架有关。正如我所说的整个东西是捆绑在一个这样的框架:'我想要抓取的页面在这里' – Asiat

+0

您需要找出它属于哪个视图模型是任何父视图模型,然后更新敲除可观察值。例如:'MainVM.SubVM()。selectedLocation(3);' –