Javascript 第一次加载页面时的选项。但是,当您调用“subjectChange”函数时,innerHTMl将被更改,并将新元素添加到页面中,但这些新元素没有附加eventListeners。您还需要将eventListeners附加到这些元素。像这样:
第一次加载页面时的选项。但是,当您调用“subjectChange”函数时,innerHTMl将被更改,并将新元素添加到页面中,但这些新元素没有附加eventListeners。您还需要将eventListeners附加到这些元素。像这样:Javascript 第一次加载页面时的选项。但是,当您调用“subjectChange”函数时,innerHTMl将被更改,并将新元素添加到页面中,但这些新元素没有附加eventListeners。您还需要将eventListeners附加到这些元素。像这样: ,javascript,html,css,dom,Javascript,Html,Css,Dom,第一次加载页面时的选项。但是,当您调用“subjectChange”函数时,innerHTMl将被更改,并将新元素添加到页面中,但这些新元素没有附加eventListeners。您还需要将eventListeners附加到这些元素。像这样: function subjectChange (){ var subjectName = document.getElementsByName('subject-name'); var que
function subjectChange (){
var subjectName = document.getElementsByName('subject-name');
var questionSel = document.getElementsByClassName('options-container')[0];
var checkBoxlength = 0;
for(var i = 0; i < subjectName.length; i++) {
if(subjectName[i].checked){
checkBoxlength++
}
}
if(checkBoxlength <= 3) {
questionSel.innerHTML = `
<label class="option">
<input type="radio" class="radio first" value="5" name="question" checked>
<span>5</span>
< /label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question" checked>
<span>10</span>
</label>
`;
} else if(checkBoxlength >= 4) {
questionSel.innerHTML = `
<label class="option">
<input type="radio" class="radio first" value="5" name="question" checked>
<span>5</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question" checked>
<span>10</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="15" name="question" checked>
<span>15</span>
</label>
`;
}
start(); //this will add eventListeners to all the elements again
}
function start(){
const selectBox = document.querySelectorAll('.select-box')
for(var i = 0; i < selectBox.length; i++) {
const selected = selectBox[i].querySelector('.selected');
const optionsContainer = selectBox[i].querySelector('.options-container');
const optionList = selectBox[i].querySelectorAll('.option');
selected.addEventListener('click', function(){
optionsContainer.classList.toggle("active");
})
optionList.forEach( o => {
var optionListItem= o.querySelectorAll('input[type="radio"]:checked + span');
optionListItem.forEach(e => selected.innerHTML = e.innerHTML)
o.addEventListener('click', function(){
var optionListItem= o.querySelectorAll('input[type="radio"]:checked + span');
optionListItem.forEach(e => selected.innerHTML = e.innerHTML)
optionsContainer.classList.remove('active')
var firstOptionList = document.querySelector('input[type="radio"]:checked');
var secondOptionList = document.querySelectorAll('.radio.second');
secondOptionList.forEach(e => {
e.removeAttribute('checked')
if(e.value == (firstOptionList.value * 2)){
//e.checked = true;
e.setAttribute('checked','');
var optionListItem2= document.querySelectorAll('input[type="radio"]:checked + span');
const selectedTwo = document.getElementsByClassName('selected');
optionListItem2.forEach(e => selectedTwo[1].innerHTML = e.innerHTML);
console.log(e)
}
})
})
})
}
}
start();
函数主题更改(){
var subjectName=document.getElementsByName('subject-name');
var questionSel=document.getElementsByClassName('options-container')[0];
var checkBoxlength=0;
对于(变量i=0;i=4){
questionSel.innerHTML=`
5.
10
15
`;
}
start();//这将再次向所有元素添加eventListeners
}
函数start(){
const selectBox=document.querySelectorAll(“.select box”)
对于(变量i=0;i{
var optionListItem=o.querySelectorAll('input[type=“radio”]:checked+span');
optionListItem.forEach(e=>selected.innerHTML=e.innerHTML)
o、 addEventListener('单击',函数()){
var optionListItem=o.querySelectorAll('input[type=“radio”]:checked+span');
optionListItem.forEach(e=>selected.innerHTML=e.innerHTML)
optionsContainer.classList.remove('active')
var firstOptionList=document.querySelector('input[type=“radio”]:checked');
var secondOptionList=document.querySelectorAll('.radio.second');
secondOptionList.forEach(e=>{
e、 removeAttribute(“选中”)
如果(e.value==(firstOptionList.value*2)){
//e、 选中=正确;
e、 setAttribute('选中','');
var optionListItem2=document.querySelectorAll('input[type=“radio”]:checked+span');
const selectedTwo=document.getElementsByClassName('selected');
optionListItem2.forEach(e=>selectedTwo[1].innerHTML=e.innerHTML);
控制台日志(e)
}
})
})
})
}
}
start();
您能否进一步解释一下您所说的“页面加载时工作正常,但在重新检查我的复选框后,我无法从第一个复选框中选择任何选项”的意思。请尝试删除[0]来自varquestionSel@NaveenChahar当第一次显示页面时,一切正常。但当我重新检查或更新复选框时,我无法从第一次选择中选择选项list@godfather显然你可以想出一个比我上面展示的更好的方法,但是你知道为什么你的代码失败了。谢谢你的详细说明led解释。我已经用这种方法解决了我的问题。很乐意帮助!!