【问题标题】:Bootstrap Form JQuery validation message position [duplicate]Bootstrap Form JQuery验证消息位置[重复]
【发布时间】:2017-10-15 16:50:34
【问题描述】:

我正在为引导表单进行 JQuery 验证,一切似乎都正常,除了这个:

我不确定如何强制在输入上方显示性别验证消息,就像其他消息一样。

HTML代码:

   <div class="form-group">
    <label>Gender:</label>
    <label class="radio-inline" id="gender">
        <input name="radioBtn" type="radio" value="male">Male
    </label>
    <label class="radio-inline">
        <input name="radioBtn" type="radio" value="female">Female
    </label>
    </div>

JQuery 代码:

    $("#boostrapForm").validate({
    rules: {
        firstname: { required: true },
        lastname: { required: true },
        email: {
            required: true,
            email: true
        },
        occupationSelect: { required: true },
        radioBtn: { required: true }            
    },

    messages: {
        firstname: "Please enter your firstname",
        lastname: "Please enter your lastname",
        email: "Please enter a valid email address",
        occupationSelect: "Please make a selection",
        radioBtn: "Please make a selection"
    }
});

欢迎任何帮助!

【问题讨论】:

  • 您必须在 Bootstrap 中使用自定义 highlightunhighlighterrorPlacement 等函数。搜索 SO,查看上面链接的副本,并在发布问题之前尝试自己解决此问题。谢谢。

标签: jquery twitter-bootstrap jquery-validate


【解决方案1】:

最好创建一个 div 来在每个字段后打印一条错误消息。您可以为 radioBtn 编写另一个函数。

HTML:

<div class="form-group">
        <label>Gender:</label>
        <label class="radio-inline" id="gender">
            <input name="radioBtn" type="radio" value="male">Male
        </label>
        <label class="radio-inline">
            <input name="radioBtn" type="radio" value="female">Female
        </label>
        </div>

        <div class="messageContainer"></div>

javascript:

$(document).ready(function() {
    $('#boostrapForm').formValidation({
        framework: 'bootstrap',
        err: {
            container: function($field, validator) {
                return $field.parent().next('.messageContainer');
            }
        },

        fields: {
            radioBtn: {
                validators: {
                    notEmpty: {
                        message: 'This is required field and cannot be empty'
                    },

                }
            },
        }
    });
});

【讨论】:

  • .formValidation() 方法/插件与 jQuery Validate 插件完全无关,这是 OP 所要求的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
相关资源
最近更新 更多