jQuery validation moves bootstrap button add-on

雨燕双飞 提交于 2019-12-13 01:36:09

问题


I am using the jQuery Validation plugin and it works great - however, on a field where I am using a bootstrap button add-on, when the required alert is shown, the button doesn't move with the text box - I've replicated the issue here: jsfiddle

Here is the html:

<form id="frmPO" action="" method="get" runat="server">
<div class="form-group input-sm">
            <div class="row">
                <div class="col-sm-6">
                    <label for="txtClientContract">
                        Client Contract</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="txtClientContract" runat="server" required/>
                        <span class="input-group-btn">
                            <button class="btn btn-default" type="button" id="btnClientContracts" >
                                ...</button>
                        </span>
                    </div>
                  </br>
           <div class="row">
                <div class="form-group col-sm-4">
                    <button id="btnSavePO" type="button" class="btn btn-default">
                        Create Purchase Order</button>
                </div>
            </div>
                </div>
  </div>
  </form>

Any help would be much appreciated, thanks


回答1:


It is because the label inserted by validation plugin between input and span. You can add label for error message manually after div tag like this:-

 <label for="txtClientContract" generated="true" class="error"></label>
 //          ^ this is id for input 

Check Demo




回答2:


This is how I fixed it:

$(".input-group").find("label.error").detach().insertAfter($(".input-group"));

Adding the above line of code before alert("not valid!"); solved your issue.

Here is the JSFiddle demo

Basically what this does is it finds the error message and moves it after the button group so as not to break the structure using JQuery.



来源:https://stackoverflow.com/questions/32200856/jquery-validation-moves-bootstrap-button-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!