twitter-bootstrap 在引导程序中的复选框中设置所需的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18015185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:49:16  来源:igfitidea点击:

Set the property required at checkbox in bootstrap

htmlformsvalidationtwitter-bootstrapcheckbox

提问by Dany

I have a simple form with a text-input and two checkbox and I need to set the property "required" at checkbox (I need that at least one of that checkbox is checked).

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

<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>

Please, Any suggests?

请问,有什么建议吗?

EDIT

编辑

I tried to use jqBootstrapValidation library in the follow way, but it doesn't works: 1. add library 2. Add this into <head>tag:

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

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

3 this is my form code:

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>

采纳答案by Arbaoui Mehdi

A JQuery validation plugin for bootstrap forms:

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

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

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

回答by Pascal Spruit

You are trying to run a validation with php while the page has already been loaded. For the validation to work each time you check another box, a script needs to run again to perform the validation. This is much easier to achieve with a jQuery script that executes each time a checkbox is clicked.

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