我有以下代码。当用户点击“添加另一项运动”时,我试图获取它,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 -->
答案 0 :(得分: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;
}
如果你将它添加到页面底部的脚本标签,就在结束体标记之前它应该有效。