2017-11-28 14 views
0

私がしようとしているのは、breed_selectorドロップダウンメニューを開き、breed1を選択してからクリックし、次の品種に移動してクリックして続行することです。この3回、残念ながら、それは犬とクリックの最初の品種を選択するだけで、3つすべてを選択することはありません。Javascriptもう一度クリックして実行してください

おかげ

(function() { 

    var x = document.getElementById("breed_selector).options; 
    for(var i=0;i<x.length;i++){ 
     if(x[i].text=="Labrador"){ 
      x[i].selected=true; 
document.getElementsByClassName("shop")[0].click(); 
      break; 
    } 
} 

     var x = document.getElementById("breed_selector").options; 
    for(var i=0;i<x.length;i++){ 
     if(x[i].text=="poodle"){ 
      x[i].selected=true; 
document.getElementsByClassName("shop")[0].click(); 



})(); 

現時点では、それだけで何もしていませんが、

私は

var = document.getElementById("breed_selector").options; 
    for(var i=0;i<x.length;i++){ 
     if(x[i].text=="poodle", "Labrador", "pug"){ 
      x[i].selected=true; 
document.getElementsByClassName("shop")[0].click(); 
      break; 

を試してみましたが、上記のいずれかに動作しない、任意の入力を希望偉大な、ありがとう:)

まだ動作しない更新コード

(function() { 

    var x = document.getElementById("breed_selector").options; 
    for(var i=0;i<x.length;i++){ 
     var text = x[i].text; 
     if(x[i].text === "Labrador" && text === "Pug"){ 
      x[i].selected=true; 
document.getElementsByClassName("shop")[0].click(); 
      break; 
    } 
} 


})(); 
+0

その値に基づいてのみ、特定のオプションを選択するために、あなたのような何かを試みることができます'click'イベントはバインドされていますか? –

+0

ちょっと、私は正直言ってあなたが何をしているのかということには少しも手がかりがありません。私は最近JSに入ったばかりです。最初のものの品種だけを見つけて、それらのすべてではなくクリックしているだけです。 – TedLIndsay

+0

更新されたコードでは、なぜ同じ変数を2つの異なる値と比較していますか?** && **条件でも同じですか?それは常に 'false'を与えます。 – aquaman

答えて

0

あなたはselect要素のすべてのオプションをクリックして、これを試みることができる。しかし

var breedSelector = document.getElementById("breed_selector"); 
var howMany = breedSelector.length; 
var buttonShop = document.querySelector(".shop"); 

for (var i = 0; i < howMany; i++) { 
    breedSelector.selectedIndex = i; 
    buttonShop.click(); 
} 

注意を、それが立て続けにボタンを数回クリックすること。なぜあなたは要素の `click`イベントをトリガで悩まれており、単にコードを呼び出さない

var breedSelector = document.getElementById("breed_selector"); 
var whichOnes = ["Labrador", "Poodle", "Golden"]; //whichever you want. 
var buttonShop = document.querySelector(".shop"); 

whichOnes.forEach(function(breed){ 
    breedSelector.selectedIndex = [].findIndex.call(breedSelector, function(option) { return option.textContent == breed}); 
    buttonShop.click(); 
} 
関連する問題