你好我試圖驗證選項,用戶檢查,如果他們沒有在頁面上籤任何東西,然後會顯示錯誤信息,讓他們檢查前,將繼續的Javascript驗證
這裏是我的代碼
for (var j=0;j<f10; j++)
{
if (document.pizza.field10[j].checked == true)
{ check2 ++ ; }
}// for ends heree
if (check2 == 0)
{
alert("Error!!: Please select atleast one topping for Pizza.");
document.pizza.field10.focus();
return false;
}
/*Field05 Validation Ends Here*/
和var f10 = document.pizza.field10.length ;// field05
這是總長度和f10
是變量。
我不知道什麼是錯的,即使用戶沒有檢查它仍然繼續
感謝
選擇這裏編輯是html代碼
<p>Toppings:</p>
<table width="400" class="toppings">
<tr>
<td><label>
<input type="checkbox" name="field10" value="chicken" />
Chicken</label></td>
<td><label>
<input type="checkbox" name="field10" value="beef" />
Beef</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="field10" value="green pepper" />
Green Pepper</label></td>
<td><label>
<input type="checkbox" name="field10" value="olives" />
Olives</label></td>
</tr>
<tr>
<td><label>
<input type="checkbox" name="field10" value="onions" />
Onions</label></td>
<td><label>
<input type="checkbox" name="field10" value="red pepper" />
Red Pepper</label></td>
</tr>
</table>
EDIT2:這裏是表格標籤
<form name='pizza' id='pizza' method='post'
action='https://cs.senecac.on.ca/~int222/cgi-bin/assign3.cgi'
onsubmit='return FormValidation();'>
這裏是提交按鈕
<p> <input class="button" name="Submit" type="submit" value="Submit" /> <input class="button" name="reset" type="reset" value="Reset" />
</p></div>
您可以在HTML中添加此腳本正在運行嗎?我的直覺是'field10 [j]'沒有完全按照你的意願去做 – Chris
@Chris我在上面添加了html代碼謝謝! – Ali
「f」應該在那裏嗎? 'for(var j = 0; j