2013-03-15 64 views
1

我有以下代碼。當用戶點擊「添加另一項運動」時,我試圖獲得它,kpsport1將增加到2,3,4等,具體取決於用戶點擊的次數。
另外,當他們選擇一項運動時,我希望將「選擇你的運動」(formSectionHeader)替換爲他們選擇的運動。儘管在JS部分工作了幾天,但仍然有很多問題需要解決。感謝任何見解!非常感謝!!如何根據下拉選項更改跨度的內容

<div id="copy"> 
    <div id="personalInfo"> 
       <h1>Sporting Experience</h1> 

       <form action="" name="signUp-pg1" id="signUp-pg1"> 

        <div class="singleSportWrap"> 
         <h2 class="formSectionHeader"><strong>CHOOSE YOUR SPORT</strong> <span>you can add another sport later.</span></h2> 

         <div class="singleSport"> 

        <div id="KPspecialityPosition" class="formSection"> 

         <div id="sportSelectField" class="selectStyled"> 
           <select class="longField styled KPsportSelecter" name="KPsport1" id="KPsport1"> 
           <option value="default">Sport</option> 
           <option value="basketball">Basketball</option> 
           <option value="soccer">Soccer</option> 
           <option value="football">Football</option> 
           <option value="baseball">Baseball</option> 
           <option value="hockey">Hockey</option> 
          </select> 

          <div class="checkboxInput"><input type="checkbox" name="primarySport" value="primarySport"><span>This my primary sport</span></div> 

         </div><!-- //selectStyled --> 


              </div><!-- //formSection --> 
       </div><!-- //singleSport --> 
       </div><!-- //singleSportWrap --> 


       <p id="addAnotherSport" class="addMoreBtn">Add Another Sport</p> 

       <div id="formStepControls"> 
        <a id="saveForm">Save for Later</a> 
        <a href="sign-up-3.php" id="proceedForm">Proceed to Vouching</a> 
       </div><!-- //formStepControls --> 

      </form> 


    </div><!-- personal info --> 

</div><!-- copy --> 
+1

這與PHP或HTML5有什麼關係?另外,你試過的JavaScript在哪裏? – j08691 2013-03-15 19:14:11

回答

0

下面是一些JavaScript應該做你問的(或至少我怎麼解釋你所要求的):

document.getElementById("addAnotherSport").onclick = function() { 
    var kps = document.getElementById("KPsport1"); 

    var i = kps.selectedIndex; 
    i++; 
    if (i >= kps.options.length) i = kps.options.length - 1; 

    kps.selectedIndex = i; 

    document.getElementsByTagName("strong")[0].innerHTML = kps.options[kps.selectedIndex].text; 
} 

document.getElementById("KPsport1").onchange = function() { 
    document.getElementsByTagName("strong")[0].innerHTML = this.options[this.selectedIndex].text; 
} 

如果在此添加到腳本標籤頁面的底部恰好在關閉的body標籤之前應該起作用。

+0

謝謝!將嘗試這個並回饋給你! – lobstahcrushah 2013-03-15 19:27:32

相關問題