我在需要使用Coldfusion驗證的網頁上有複選框,我如何驗證每個組中至少有一個複選框需要回答?使用Coldfusion驗證複選框
<div name="multanswerheader" id="multanswerheader" style="width:100%;">
<div name="multanswerques" id="multanswerques">
<label class="multanswerques">Ques 1:</label>
<label class="multanswerques">Ques 2:</label>
<label class="multanswerques">Ques 3:</label>
<label class="multanswerques">Ques 4:</label>
<label class="multanswerques">Ques 5:</label>
</div>
<div name="multanswera" id="multanswera">
<div class="multanswerques"><input type="checkbox" name="multradio1" value="1" <cfif ListFind(form.multradio1,"1") GT 0>checked="checked"</cfif>>A</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio2" value="1" <cfif ListFind(form.multradio2,"1") GT 0>checked="checked"</cfif>>A</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio3" value="1" <cfif ListFind(form.multradio3,"1") GT 0>checked="checked"</cfif>>A</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio4" value="1" <cfif ListFind(form.multradio4,"1") GT 0>checked="checked"</cfif>>A</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio5" value="1" <cfif ListFind(form.multradio5,"1") GT 0>checked="checked"</cfif>>A</input></div>
</div>
<div name="multanswerb" id="multanswerb">
<div class="multanswerques"><input type="checkbox" name="multradio1" value="2" <cfif ListFind(form.multradio1,"2") GT 0>checked="checked"</cfif>>B</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio2" value="2" <cfif ListFind(form.multradio2,"2") GT 0>checked="checked"</cfif>>B</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio3" value="2" <cfif ListFind(form.multradio3,"2") GT 0>checked="checked"</cfif>>B</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio4" value="2" <cfif ListFind(form.multradio4,"2") GT 0>checked="checked"</cfif>>B</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio5" value="2" <cfif ListFind(form.multradio5,"2") GT 0>checked="checked"</cfif>>B</input></div>
</div>
<div name="multanswerc" id="multanswerc">
<div class="multanswerques"><input type="checkbox" name="multradio1" value="3" <cfif ListFind(form.multradio1,"3") GT 0>checked="checked"</cfif>>C</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio2" value="3" <cfif ListFind(form.multradio2,"3") GT 0>checked="checked"</cfif>>C</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio3" value="3" <cfif ListFind(form.multradio3,"3") GT 0>checked="checked"</cfif>>C</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio4" value="3" <cfif ListFind(form.multradio4,"3") GT 0>checked="checked"</cfif>>C</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio5" value="3" <cfif ListFind(form.multradio5,"3") GT 0>checked="checked"</cfif>>C</input></div>
</div>
<div name="multanswerd" id="multanswerd">
<div class="multanswerques"><input type="checkbox" name="multradio1" value="4" <cfif ListFind(form.multradio1,"4") GT 0>checked="checked"</cfif>>D</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio2" value="4" <cfif ListFind(form.multradio2,"4") GT 0>checked="checked"</cfif>>D</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio3" value="4" <cfif ListFind(form.multradio3,"4") GT 0>checked="checked"</cfif>>D</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio4" value="4" <cfif ListFind(form.multradio4,"4") GT 0>checked="checked"</cfif>>D</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio5" value="4" <cfif ListFind(form.multradio5,"4") GT 0>checked="checked"</cfif>>D</input></div>
</div>
<div name="multanswere" id="multanswere">
<div class="multanswerques"><input type="checkbox" name="multradio1" value="5" <cfif ListFind(form.multradio1,"5") GT 0>checked="checked"</cfif>>E</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio2" value="5" <cfif ListFind(form.multradio2,"5") GT 0>checked="checked"</cfif>>E</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio3" value="5" <cfif ListFind(form.multradio3,"5") GT 0>checked="checked"</cfif>>E</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio4" value="5" <cfif ListFind(form.multradio4,"5") GT 0>checked="checked"</cfif>>E</input></div>
<div class="multanswerques"><input type="checkbox" name="multradio5" value="5" <cfif ListFind(form.multradio5,"5") GT 0>checked="checked"</cfif>>E</input></div>
</div>
</div>
你知道如果form.checkbox1a沒有在表單中提交,這段代碼會錯誤的嗎?未經檢查的複選框不會被提交,也不是表單範圍的一部分。 –
我不太清楚你的意思是什麼......我有這些工作的形式,因爲我希望它。 –
@Zorpix - 那是因爲你在代碼的其他地方聲明瞭'form.checkbox1a'的默認值。所以它總是被定義的。刪除默認(和額外的'ListFind'代碼),如果沒有選中任何一個框,上面的代碼將會出錯。 – Leigh