2017-09-05 90 views
0

我試着用PhantomJS測試ReactJS,測試ReactJS與PhantomJS - 更改選擇值

我似乎無法觸發更改選擇事件:

   selectWidth = []; 
       selectPath = $("#pdp-width-select").first() 
       options = $("option", selectPath); 
       options.each(function() { 
        $(this).attr("selected","selected"); 
        $(this).trigger("change"); 
        selectWidth.push({ 
         text: $(this).text(), 
         value: $(this).val(), 
         stock: $("._1HQVd").text() 
        }) 
       }) 

此代碼試圖觸發更改事件和取股票價值。

但它不起作用。

我也試過這個方法:

Change the selected value of a drop-down list with jQuery

How to Change select value using PhantomJS

How to trigger event in JavaScript?

   selectWidth = []; 
       selectPath = $("#pdp-width-select").first() 
       options = $("option", selectPath); 
       options.each(function (i) { 
        selectPath[0].selectedIndex = i; 
        selectWidth.push({ 
         text: $(this).text(), 
         value: $(this).val(), 
         stock: $("._1HQVd").text() 
        }) 
       }) 

注:我不能讓它在Chrome瀏覽器開發工具工作,同時。

回答

0

溶液

   evtName = 'change'; 
       var event = document.createEvent("MouseEvents"); 
       event.initMouseEvent(evtName, true, true, window, 
        0, 0, 0, 0, 0, false, false, false, false, 0, null); 

$("select#pdp-width-select").val('83720')[0].dispatchEvent(event);