我在网页上有复选框需要使用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>
答案 0 :(得分:3)
以下是我所做过的表格中的一个例子。
<cfif isDefined("form.submit")>
<cfif form.checkbox1a EQ "" >
<p class="text-error">Please check at least one answer below</p>
</cfif>
</cfif>
<fieldset data-role="controlgroup">
<legend>*Pick some chips</legend>
<input type="checkbox" name="checkbox1a" id="checkbox1a" value="a" <cfif ListFind(form.checkbox1a,"a") GT 0>checked="checked"</cfif>/>
<label for="checkbox1a">Cheetos</label>
<input type="checkbox" name="checkbox1a" id="checkbox2a" value="b" <cfif ListFind(form.checkbox1a,"b") GT 0>checked="checked"</cfif>/>
<label for="checkbox2a">Doritos</label>
<input type="checkbox" name="checkbox1a" id="checkbox3a" value="c" <cfif ListFind(form.checkbox1a,"c") GT 0>checked="checked"</cfif>/>
<label for="checkbox3a">Fritos</label>
<input type="checkbox" name="checkbox1a" id="checkbox4a" value="d" <cfif ListFind(form.checkbox1a,"d") GT 0>checked="checked"</cfif>/>
<label for="checkbox4a">Sun Chips</label>
</fieldset>
您必须确保您的复选框具有相同的名称,但值不同。然后只需检查该名称是否有值。我在复选框之前完成了我的操作,因为我希望错误消息显示在框上方,但由于CF仅在表单提交时触发,因此它将执行第二次。
答案 1 :(得分:1)
您应该在不在ColdFusion中的客户端中执行此操作。未选中的复选框不会提交给服务器。您必须测试它们不属于服务器上的表单范围的事实。