简体   繁体   English

表格在AngularJS中始终无效

[英]Form is always invalid in AngularJS

It seems very trivial, so I'm sorry if I just missed something, but I can't figure it out. 看起来很琐碎,所以很抱歉如果我错过了一些事情,但是我无法解决。

I want to enable my button which sends data using AJAX when form is valid, but the button is always disabled. 我想启用在表单有效时使用AJAX发送数据的按钮,但该按钮始终处于禁用状态。

Have a look: 看一看:

<form name="UserReg" novalidate>

        <div class="form-group">
            <input type="text" name="user_name"
                                data-ng-model="UserName"
                                ng-maxlength=20
                                placeholder="Name"
                                required />

            <div data-ng-messages="UserReg.user_name.$error"
                 data-ng-show="UserReg.user_name.$touched">

                 <div data-ng-messages-include="angular_app/validation/form_messages.html"></div>

            </div>
        </div>


       <div class="form-group" style="padding-top:20px;">
            <button type="button" name="add_user"
                                    class="top-menu-button"
                                    data-ng-click="AddUser()"
                                    ng-disabled="UserReg.$invalid">
                <i class="fa fa-plus fa-lg padding-r-5"></i> Add User
            </button>
        </div>

</form>

Where is the problem? 问题出在哪儿?

First you should enclose the value of ng-maxlength in double quotes. 首先,您应该将ng-maxlength的值括在双引号中。 Second, Since you have required attribute on the input field, The form will become valid only if the input field has some value (type something in it & the form will become valid). 其次,由于您在输入字段上具有required属性,因此仅当输入字段具有某个值(在其中输入某些内容且该表格将变为有效)时,该表格才有效。

Otherwise if not necessary remove the required attribute from the input field. 否则,如果没有必要,请从输入字段中删除required属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM