Javascript:Onload如果复选框被选中,更改li类 [英] Javascript: Onload if checkbox is checked, change li class
问题描述
我在javascript侧有这个代码:
I have this code on javascript side:
function changeSelectionStyle(id) {
if(document.getElementById(id).className != 'yes'){
document.getElementById(id).className = 'yes';
}
else{document.getElementById(id).className = '';}
}
在html上:
<ul>
<li id="selectedAuthorities-4_1li" class="">
<input type="checkbox" id="selectedAuthorities-4_1" name="selectedAuthorities" value="ROLE_ADD_COMMENT_TO_CV" checked="checked" onload="changeSelectionStyle(this.id + 'li');" onclick="changeSelectionStyle(this.id + 'li'); checkFatherSelection(this.id);">
<a href="#" onclick="document.getElementById('selectedAuthorities-4_1').click(); return false;"> Agregar comentario <samp><b></b>Permite agregar comentario en el detalle.</samp>
</a>
</li>
我不能得到的工作是当复选框被选中,加载, JS应该检查它,并将 class =yes
分配给li,这里:< li id =selectedAuthorities-4_1liclass = >
Well what I cant get work is when the checkbox is selected, on load, the JS should check it, and assign class="yes
" to the li, here: <li id="selectedAuthorities-4_1li" class="">
我应该添加到我的原始JS函数,注意,liid不是静态的,它们是动态的,生成html li id。
What should I add to my original JS function to make that possible. Note that li ids are not static, they are dynamic, the html li id is generated.
解决方案的过程(逻辑):
Process for the solution (logic):
加载时的事件,检查是否检查了X id的输入,如果是,则使用JS函数changeSelectionStyle更新父li。因为我现在的问题是,当我选择一个输入(检查),li颜色是切换,但当它加载选择/检查的输入,颜色保持不变,并且其改变颜色,当我(取消选中它 - 错误的效果)
Event on load, check if the input with X id is checked, if it is, update the parent li with the JS function changeSelectionStyle. Because the problem that I have right now, is that when I select an input (check it), the li color is switching, but when it loads with a selected/checked input, the color remains untouched, and its changing the color when I click the input (uncheck it - wrong effect)
推荐答案
window.onload=function(){
var inputFields=document.getElementsByTagName('input');
var checkBoxes[];
for(var i=0;i<inputFields.length;i++)
if(inputFields[i].type=='checkbox'&&inputFields[i].checked=='checked'&& inputFields[i].parentNode.tagName=='LI')
inputFields[i].parentNode.className='yes';
}
这里我们去..
这篇关于Javascript:Onload如果复选框被选中,更改li类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!