全選 單選 控制元件的判斷
阿新 • • 發佈:2020-10-09
checkbox和radio 這兩個控制元件比較的特殊
如果我們想對是否選中的控制元件進行判斷 並提示要選擇的項,那我們得如何來做呢
<form name="form" id="form">
<input type="checkbox" name="sex" value="男">男
<input type="checkbox" name="sex" value="女">女
</form>
window.οnlοad=function() { varoForm=document.getElementById("form1"); varoList=oForm.love;//這裡返回一個集合 //它有length屬性你可以去for遍歷它然後一個一個的進行判斷 }
下面我們來編寫一個完整一點的吧.....
window.οnlοad=function() { //獲取 varoForm=document.getElementById("form1"); varoList=oForm.love; varstr="上網,旅遊"; vararr=str.split(","); for(vari=0;i<arr.length;i++) { for(varj=0;j<oList.length;j++) { if(arr[i]==oList[j].value) { oList[j].setAttribute("checked","checked"); } } } }
轉載於:https://blog.51cto.com/771541213/1760352