2016-09-21 156 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);' –