【问题标题】:Error message breaks layout when displayed错误消息在显示时会破坏布局
【发布时间】:2015-06-12 10:18:01
【问题描述】:

我的页面上有以下字段

由以下 HTML 构成

<div style="margin-bottom: 10px" class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
   @Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "Choose a username (max 25)", Maxlength = "25", autocomplete = "off" })
</div>

非常基本的东西,我目前在我的应用程序中嵌入了 jquery 表单验证,如下所示:

我已经删除了除此之外的所有字段。

 $('#registrationForm').validate({
    rules: {
        "Username": "required",

    },
    messages: {
        "Username": "Please enter your chosen Username",

    }
});

当我将该字段留空并按提交时,它看起来像这样

如您所见,用户字形图标的高度已增加以适应错误消息。

呈现的 HTML 如下所示:

<div class="input-group" style="margin-bottom: 10px">
 <span class="input-group-addon">
  <i class="glyphicon glyphicon-user"></i>
 </span>
<input id="Username" class="form-control" type="text" value="" placeholder="Choose a username (max 25)" name="Username" data-val-required="The Username field is required." data-val="true" autocomplete="off" maxlength="25">
</div>

谁能说明为什么会发生这种情况?

更新

这是显示错误消息时呈现的内容

<div class="input-group" style="margin-bottom: 10px">
 <span class="input-group-addon">
  <i class="glyphicon glyphicon-user"></i>
 </span>
<input id="Username" class="form-control error" type="text" value="" placeholder="Choose a username (max 25)" name="Username" data-val-required="The Username field is required." data-val="true" autocomplete="off" maxlength="25">
<label class="error" for="Username">Please enter your chosen Username</label>
</div>

** 向 .Error 类添加其他样式后更新 **

【问题讨论】:

  • 你能检查元素并显示生成的&lt;span&gt;吗?

标签: javascript jquery html jquery-form-validator


【解决方案1】:

执行此操作时会生成一个额外的&lt;span&gt;!我想应该是.error。只需给它一个position: absolute;

.error {position: absolute; right: 0; width: 100%; bottom: -1em;}

或者只是从.input-group中取出错误文本:

<div style="margin-bottom: 10px" class="input-group">
  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
</div>
@Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "Choose a username (max 25)", Maxlength = "25", autocomplete = "off" })

【讨论】:

  • 我添加了绝对位置,但错误消息与文本框出现在同一行,但在最后,它全部被压扁(无法阅读)。如果我使用您提供的 HTML,则 glyphicon 用户不再位于框旁边,而是显示在顶部。这不是我想要的
  • @ScottAtkinson 请通过检查那里的元素来更新生成的 HTML。我们可以轻松地将其向前推进。请记住,您只需要使用一个,绝对位置或 div 之外的 HTML。
  • 我已将样式 .error 更新为绝对位置
  • 好。正如预期的那样。当你给予绝对的时候会发生什么?发布截图!
  • 错误消息出现在文本框的右侧,但它全部被压扁且不可读。
【解决方案2】:

在错误信息中添加如下样式

position : absolute

【讨论】:

    猜你喜欢
    • 2018-06-14
    • 2016-11-02
    • 1970-01-01
    • 2019-06-03
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多