0

私はjavascript側にこのコードを持っています:

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_1li" class="">

それを可能にするために、元のJS関数に何を追加する必要がありますか。li idは静的ではなく、動的であり、htmlliidが生成されることに注意してください。

ソリューションのプロセス(ロジック):

ロード時のイベントで、X idの入力がチェックされているかどうかを確認します。チェックされている場合は、JS関数changeSelectionStyleを使用して親liを更新します。私が今抱えている問題は、入力を選択する(チェックする)と、liの色が切り替わるということですが、選択/チェックした入力でロードすると、色はそのままになり、I入力をクリックします(チェックを外す-間違った効果)

4

2 に答える 2

2

このコードを試してください:

window.onload=function(){
 const checkBoxes=document.querySelectorAll("input[type='checkbox']");
 for(var i=0; i < checkBoxes.length; i++) 
 {
    if(checkBoxes[i].checked=='checked')
       checkBoxes[i].parentNode.className='yes';
 }
}
于 2021-09-06T21:43:43.963 に答える
0
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';
}

どうぞ..

于 2014-12-05T09:57:31.930 に答える