????<body>
????????<form>
??????????請選擇你愛好:<br>
??????????<input?type="checkbox"?name="hobby"?id="hobby1">??音樂
??????????<input?type="checkbox"?name="hobby"?id="hobby2">??登山
??????????<input?type="checkbox"?name="hobby"?id="hobby3">??游泳
??????????<input?type="checkbox"?name="hobby"?id="hobby4">??閱讀
??????????<input?type="checkbox"?name="hobby"?id="hobby5">??打球
??????????<input?type="checkbox"?name="hobby"?id="hobby6">??跑步?<br>
??????????<input?type="button"?value?=?"全選"?onclick?=?"checkall();">
??????????<input?type="button"?value?=?"全不選"?onclick?=?"clearall();">
??????????<p>請輸入您要選擇愛好的序號,序號為1-6:</p>
??????????<input?id="wb"?name="wb"?type="text"?>
??????????<input?name="ok"?type="button"?value="確定"?onclick?=?"checkone();">
????????</form>
????????<script?type="text/javascript">
????????function?checkall(){
????????????var?hobby?=?document.getElementsByTagName("input");
????????????for(i=0;?i<hobby.length;?i++)
????????????{
????????????????hobby[i].checked=true;
????????????}
??????????//?任務1?
???????????
????????}
????????function?clearall(){
????????????var?hobby?=?document.getElementsByName("hobby");
????????????for(i=0;?i<hobby.length;?i++)
????????????{
????????????????hobby[i].checked=false;
????????????}
?????????//?任務2????
????????????
????????}
????????function?checkone(){
????????????var?j=document.getElementById("wb").value;
????????????function?isInteger(obj)?
????????????{
????????????????return?Math.floor(obj)===obj;
????????????}
????????????var?t=isInteger(j);
????????????if(t=false)
????????????{
????????????????alert("請輸入1和6之間的整數!");
????????????}else?if(t>6?||?t<1)
????????????{
????????????????alert("請輸入1和6之間的整數!")
????????????}else{
????????????????hobby[t].checked=true;
????????????}
?????????//?任務3
????????
????????}
????????
????????</script>
????</body>
2017-02-16
? ? ??function?checkone(){
????????????var?j=document.getElementById("wb").value;
????????????function?isInteger(obj)?
????????????{
????????????????return?Math.floor(obj)===obj;
????????????}
????????????var?t=isInteger(j);
????????????if(t=false)
????????????{
????????????????alert("請輸入1和6之間的整數!");
????????????}
? ? ? ? ? ? else{
????????????????????????if(j>6||j<1)
????????????????????????{ ? ?alert("請輸入1和6之間的整數!")
????????????????????????}
????????????????else
????????????????????{
????????????????????????hobby[j-1].checked=true;
????????????????????}
????????????? ? }
?????????//?任務3
?????????
????????}
程序有問題,t只是false或者true,不是一個值,而且數組的索引值從0開始,j必須減一。
2017-02-16
或許=獲取 ? 剛剛打錯字了
2017-02-16
var?j=document.getElementById("wb").value; ? 或許的value是字符串,需要用parseInt()轉為數字類型;
??
以下:
? ? ? function checkone(){
? ? ? ? ? ? var hobby = document.getElementsByName("hobby");
? ? ? ? ? ? var t=parseInt(document.getElementById("wb").value);
? ? ? ? ? ? if(t<=6 && t>=1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? hobby[(t-1)].checked=true;
? ? ? ? ? ? }else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? alert("請輸入1和6之間的整數!");
? ? ? ? ? ? }
? ? ? ? ?// 任務3 ? ? ? ?
? ? ? ? }