2016-10-15 7 views
0

私は現在、ドロップダウンメニューでテキストを選択した後、並んで表示されるテキストの3つの翻訳を取得しようとしています。これまでのところ、問題なく動作する最初のメニューがありますが、2番目と3番目のメニューは協力していません。私はこれが何らかのネスティングエラーであると仮定しますが、私はそれを理解できません。どのように3つのすべてのフィールドセットのjavascriptを動作させるのですか?ここで並んでいる3つのフィールドセット

<html> 
<head> 
<style> 
body 
{ 
    background-image:url('gradient1.jpg'); 
    background-repeat:repeat-x; 
} 
.ex 
{ 
    margin:auto; 
    width:90%; 
    padding:10px; 
    border:outset; 
} 
select 
{ 
    display:inline; 
    cursor:pointer; 
} 
form{ 
    display:inline-block; 
    width: 550px; 
} 

</style> 
</head> 
<body> 
    <div class="ex"> 
     <form action="#" method="post" id="demoForm"> 

      <fieldset> 
       <legend>Select Translation</legend> 
       <p> 
       <select id="scripts" name="scripts"> 
        <option value="scroll">Orignal Greek</option> 
        <option value="tooltip">Original Latin</option> 
        <option value="con_scroll">English Translation</option> 
       </select> 
       <br> 
       <p> 
       <input type="button" id="showVal" value="Select" /> 
       </p>  
       <output type="text" size="30" name="display" id="display" /> 
      </p> 

      </fieldset> 

     </form> 
     <form action="#" method="post" id="demoForm2"> 

      <fieldset> 
       <legend>Select Translation</legend> 
       <p> 
       <select id="scripts" name="scripts"> 
        <option value="scroll">Orignal Greek</option> 
        <option value="tooltip">Original Latin</option> 
        <option value="con_scroll">English Translation</option> 
       </select> 
       <br> 
      <p> 
      <input type="button" id="showVal" value="Select" /> 
      </p>  
      <output type="text" size="30" name="display" id="display" /> 
      </p> 

    </fieldset> 

    </form> 
    <form action="#" method="post" id="demoForm3"> 

     <fieldset> 
      <legend>Select Translation</legend> 
      <p> 
       <select id="scripts" name="scripts"> 
        <option value="scroll">Orignal Greek</option> 
        <option value="tooltip">Original Latin</option> 
        <option value="con_scroll">English Translation</option> 
       </select> 
       <br> 
       <p> 
       <input type="button" id="showVal" value="Select" /> 
       </p>  
       <output type="text" size="30" name="display" id="display" /> 
       </p> 

     </fieldset> 

    </form> 
</div> 
</body> 
<script> 
(function() { 

    // get references to select list and display text box 
    var sel = document.getElementById('scripts'); 
    var el = document.getElementById('display'); 



    function getSelectedOption(sel) { 
     var opt; 
     for (var i = 0, len = sel.options.length; i < len; i++) { 
      opt = sel.options[i]; 
      if (opt.selected === true) { 
       break; 
      } 
     } 
     return opt; 
    } 

    // assign onclick handlers to the buttons 
    document.getElementById('showVal').onclick = function() { 
     el.value = sel.value;  
    } 

    document.getElementById('showTxt').onclick = function() { 
     // access text property of selected option 
     el.value = sel.options[sel.selectedIndex].text; 
    } 

    document.getElementById('doLoop').onclick = function() { 
     var opt = getSelectedOption(sel); 
     el.value = opt.value; 
    } 

}()); 
// immediate function to preserve global namespace 
</script> 

答えて

1

はvanillaJSでのソリューションです:私が持っているもの

[].forEach.call(document.querySelectorAll(".demoForm"), function(element) { 
 
    element.querySelector("#showVal").addEventListener("click", function(event) { 
 
    var dropdown = element.querySelector("#scripts"); 
 
    var value = dropdown.options[dropdown.selectedIndex].text; 
 
    element.querySelector("#display").innerHTML = value; 
 
    }) 
 
});
<div class="ex"> 
 
    <form action="#" method="post" class="demoForm"> 
 

 
    <fieldset> 
 
     <legend>Select Translation</legend> 
 
     <p> 
 
     <select id="scripts" name="scripts"> 
 
      <option value="scroll">Orignal Greek</option> 
 
      <option value="tooltip">Original Latin</option> 
 
      <option value="con_scroll">English Translation</option> 
 
     </select> 
 
     <br> 
 
     <div class="inline"> 
 
      <p> 
 
      <input type="button" id="showVal" value="Select" /> 
 
      </p> 
 
      <span id="display"></span> 
 
     </div> 
 

 
    </fieldset> 
 

 
    </form> 
 
    <form action="#" method="post" class="demoForm"> 
 

 
    <fieldset> 
 
     <legend>Select Translation</legend> 
 
     <p> 
 
     <select id="scripts" name="scripts"> 
 
      <option value="scroll">Orignal Greek</option> 
 
      <option value="tooltip">Original Latin</option> 
 
      <option value="con_scroll">English Translation</option> 
 
     </select> 
 
     <div class="inline"> 
 
      <p> 
 
      <input type="button" id="showVal" value="Select" /> 
 
      </p> 
 
      <span id="display"></span> 
 

 
     </div> 
 
    </fieldset> 
 

 
    </form> 
 
    <form action="#" method="post" class="demoForm"> 
 

 
    <fieldset> 
 
     <legend>Select Translation</legend> 
 
     <p> 
 
     <select id="scripts" name="scripts"> 
 
      <option value="scroll">Orignal Greek</option> 
 
      <option value="tooltip">Original Latin</option> 
 
      <option value="con_scroll">English Translation</option> 
 
     </select> 
 
     <br> 
 
     <div class="inline"> 
 
      <p> 
 
      <input type="button" id="showVal" value="Select" /> 
 
      </p> 
 
      <span id="display"></span> 
 

 
     </div> 
 
    </fieldset> 
 

 
    </form> 
 
</div>

+0

は – Gabs00

+0

は、コードを編集動作します。それは今働く。 – Sreekanth

0

IDは一意である必要があり、あなたはID showValと3つのidスクリプトを持つ要素、および3を持っています。

var sel = document.getElementById('scripts') 
var el = document.getElementById('display'); 

正確に1つの要素を返し、IDを持つていないすべての要素、私はidの3について

var sel = document.querySelectorAll('#scripts'); 
var el = document.querySelectorAll('#display'); 

でこれを置き換えでは、各ボタンに異なるIDを使用するためのもののように見えるshowVal:showVal, showTxt, doLoop

3つのオンクリックを別の方法で実行しようとしていたとしますか?私は当初、私は全体のコードをリファクタリングすることを選んだの修正はしなかったものの 次の二つのボタンは表示されません

(function() { 
 

 
    // get references to select list and display text box 
 
    var sel = document.querySelectorAll('#scripts'); 
 
    var el = document.querySelectorAll('#display'); 
 
\t \t var buttons = document.querySelectorAll('input[type="button"'); 
 
    
 
\t \t sel.forEach(function(elem, i){ 
 
    \t buttons[i].onclick = function(e){ 
 
     \t el[i].value = elem.value; 
 
     }; 
 
    }) 
 
}()); 
 
// immediate function to preserve global namespace
.ex 
 
{ 
 
    margin:auto; 
 
    width:90%; 
 
    padding:10px; 
 
    border:outset; 
 
} 
 
select 
 
{ 
 
    display:inline; 
 
    cursor:pointer; 
 
} 
 
form{ 
 
    display:inline-block; 
 
    width: 550px; 
 
}
<div class="ex"> 
 
     <form action="#" method="post" id="demoForm"> 
 

 
      <fieldset> 
 
       <legend>Select Translation</legend> 
 
       <p> 
 
        <select id="scripts" name="scripts"> 
 
         <option value="scroll">Orignal Greek</option> 
 
         <option value="tooltip">Original Latin</option> 
 
         <option value="con_scroll">English Translation</option> 
 
        </select> 
 
        <br> 
 
        <p> 
 
        <input type="button" id="showVal" value="Select" /> 
 
        </p>  
 
        <input type="text" size="30" name="display" id="display" /> 
 
       </p> 
 

 
      </fieldset> 
 

 
     </form> 
 
     <form action="#" method="post" id="demoForm2"> 
 

 
      <fieldset> 
 
       <legend>Select Translation</legend> 
 
       <p> 
 
       <select id="scripts" name="scripts"> 
 
        <option value="scroll">Orignal Greek</option> 
 
        <option value="tooltip">Original Latin</option> 
 
        <option value="con_scroll">English Translation</option> 
 
       </select> 
 
       <br> 
 
      <p> 
 
      <input type="button" id="showTxt" value="Select" /> 
 
      </p>  
 
      <input type="text" size="30" name="display" id="display" /> 
 
      </p> 
 

 
    </fieldset> 
 

 
    </form> 
 
    <form action="#" method="post" id="demoForm3"> 
 

 
     <fieldset> 
 
      <legend>Select Translation</legend> 
 
      <p> 
 
       <select id="scripts" name="scripts"> 
 
        <option value="scroll">Orignal Greek</option> 
 
        <option value="tooltip">Original Latin</option> 
 
        <option value="con_scroll">English Translation</option> 
 
       </select> 
 
       <br> 
 
       <p> 
 
       <input type="button" id="doLoop" value="Select" /> 
 
       </p>  
 
       <input type="text" size="30" name="display" id="display" /> 
 
       </p> 
 

 
     </fieldset> 
 

 
    </form> 
 
</div>

+0

ねえ、あなたは素晴らしいです。どうもありがとうございます! –

+0

@ D.Cは受け入れることを忘れないでください – Gabs00

関連する問題