2

我有一个带有文本输入和两个复选框的简单表单,我需要在复选框处设置“必需”属性(我需要至少选中一个复选框)。

<div class="control-group">
    <label class="control-label" for="inputEmail"><?= $this->labelfieldEmail; ?></label>
    <div class="controls">
        <input type="text" name="email" id="emailField"
        placeholder="Email" required>
    </div>
</div>

<div class="control-group">
    <label class="control-label" for="productField"><?= $this->labelfieldProduct; ?></label>
    <div class="controls" >
        <input type="checkbox" id="inlineCheckbox1" value="widget" name="product[]" <?php if ($this->selectedProduct == "widget") { echo "checked"; }?>/> 
        Widget for website
        <br/>
        <input type="checkbox" id="inlineCheckbox2" value="app" name="product[]" <?php if ($this->selectedProduct == "app") { echo "checked"; }?>/> 
        App mobile
    </div>
</div>

请问,有什么建议吗?

编辑

我尝试按以下方式使用 jqBootstrapValidation 库,但它不起作用: 1. 添加库 2. 将其添加到<head>标签中:

<script>
$(function () { 
    $("input,select,textarea").not("[type=submit]").jqBootstrapValidation(); 
});
</script>

3 这是我的表单代码:

<form class="form-horizontal" method="POST" action="<?= $this->baseUrl($this->pathBusinessThankyou);?>">
    <div class="row-fluid">

        <div class="row-fluid">
            <br />
            <div class="control-group">
                <label class="control-label" for="inputName"><?= $this->labelfieldName; ?></label>
                <div class="controls">
                    <input type="text" id="fullnameField" name="fullname" required />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputEmail"><?= $this->labelfieldEmail; ?></label>
                <div class="controls">
                    <input type="email" id="emailField" name="email" required />
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="productField"><?= $this->labelfieldProduct; ?></label>
                <div class="controls" >
                    <label class="checkbox">
                        <input type="checkbox" 
                        data-validation-minchecked-message="<?= $this->validationOneProduct; ?>" 
                        data-validation-minchecked-minchecked="1" value="widget" name="product[]" 
                        <?php if ($this->selectedProduct == "widget") { echo "checked"; }?> />
                        Widget per il tuo sito
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" value="app" name="product[]" aria-invalid="false"
                        <?php if ($this->selectedProduct == "app") { echo "checked"; }?> />
                        App per il tuo hotel
                    </label>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="inputEmail"><?= $this->labelfieldNote; ?></label>
                <div class="controls">
                    <textarea rows="3" name="comment" id="commentField"></textarea>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <button type="submit" class="btn input-block-level btn-large btn-success">
                        <?= $this->labelSendRequest; ?>
                        <i class="icon-ok icon-white"></i>
                    </button>
                </div>
            </div>  
        </div>
    </div>
</form>
4

2 回答 2

2

用于引导表单的 JQuery 验证插件:

http://reactiveraven.github.io/jqBootstrapValidation/

于 2013-08-02T11:18:44.093 回答
0

您正在尝试在页面已加载时使用 php 运行验证。为了使验证在您每次选中另一个框时都能正常工作,需要再次运行脚本来执行验证。使用每次单击复选框时执行的 jQuery 脚本来实现这一点要容易得多。

于 2013-08-02T11:23:42.813 回答