如何禁用提交按钮,直到表格在Angular 12中有效?

发布于 2025-02-03 00:33:44 字数 978 浏览 4 评论 0原文

login.component.html

<form (ngSubmit)="submitLogin()" #loginForm="ngForm">

<div class="form-group">
  <label for="exampleInputEmail1">Email address</label>
  <input type="email" class="form-control" id="email" name="email" [(ngModel)]="form.email" [ngModelOptions]="{standalone: true}" required>
</div>

<div class="form-group">
  <label for="exampleInputPassword1">Password</label>
  <input type="password" class="form-control" id="password" name="password" [(ngModel)]="form.password" [ngModelOptions]="{standalone: true}" required>
</div>

<button type="submit" class="btn btn-primary" [disabled]="!loginForm.form.valid">Submit</button>

</form>

我是Angular的新手,我试图禁用提交按钮,直到表单输入字段为空白,但在此显示启用,但如果我像“ loginform.form.form.form.form.valid”编码,然后提交按钮显示put 喜欢“!loginform.form.valid”然后再次显示启用提交按钮。那么,如何解决此问题?请帮我。

谢谢

login.component.html

<form (ngSubmit)="submitLogin()" #loginForm="ngForm">

<div class="form-group">
  <label for="exampleInputEmail1">Email address</label>
  <input type="email" class="form-control" id="email" name="email" [(ngModel)]="form.email" [ngModelOptions]="{standalone: true}" required>
</div>

<div class="form-group">
  <label for="exampleInputPassword1">Password</label>
  <input type="password" class="form-control" id="password" name="password" [(ngModel)]="form.password" [ngModelOptions]="{standalone: true}" required>
</div>

<button type="submit" class="btn btn-primary" [disabled]="!loginForm.form.valid">Submit</button>

</form>

I am new in angular and I trying to disabled submit button until form input field is blank but here it showing enable but if I code like "loginForm.form.valid" then the submit button show disabled after put ! like "!loginForm.form.valid" then again it show enable submit button. So, How can I fix this issue? Please help me.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路黄昏 2025-02-10 00:33:44

方法1:

尝试 [disabled] =“!

并添加如下所示的禁用CS。

button:disabled{
  cursor:not-allowed;
  opacity:0.5
}

方法2

在html使用中使用get方法

get form():FormGroup{
  return this.loginForm
}

[disabled] =“!form.valid”

Method 1:

Try [disabled]="!loginForm.valid" dont use form key word.

And add disabled css like below.

button:disabled{
  cursor:not-allowed;
  opacity:0.5
}

Method 2

Use get method in component

get form():FormGroup{
  return this.loginForm
}

In HTML use [disabled]="!form.valid"

半边脸i 2025-02-10 00:33:44

如果其他答案不起作用,请尝试一下。

<button type="submit" class="btn btn-primary" [disabled]="!form.password || !form.email">Submit</button>

Try this if the other answers don't work.

<button type="submit" class="btn btn-primary" [disabled]="!form.password || !form.email">Submit</button>
勿挽旧人 2025-02-10 00:33:44

您可以将其绑定到函数以使其自身更新。

模板:

[disabled]="invalidForm()"

TS:

public invalidForm() {
  return !loginForm.form.valid;
}

You can bind it to a function to make it update itself.

Template:

[disabled]="invalidForm()"

Ts:

public invalidForm() {
  return !loginForm.form.valid;
}
述情 2025-02-10 00:33:44

尝试 -

<button type="submit" class="btn btn-primary [disabled]="loginForm.invalid">Submit</button>

<button type="submit" class="btn btn-primary" [disabled]="!loginForm?.valid">Submit</button>

Try to -

<button type="submit" class="btn btn-primary [disabled]="loginForm.invalid">Submit</button>

or

<button type="submit" class="btn btn-primary" [disabled]="!loginForm?.valid">Submit</button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文