0

我的网页上有需要使用 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>
4

2 回答 2

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 仅在表单提交时触发,它将执行第二个。

于 2013-07-09T19:29:47.643 回答
1

您应该在客户端而不是在 ColdFusion 中执行此操作。未选中的复选框不会提交给服务器。您必须测试它们不属于服务器上表单范围的事实。

于 2013-07-10T17:04:49.710 回答