【问题标题】:How to uncheck HTML box if user hits cancel on prompt in Angular change event如果用户在 Angular 更改事件中点击取消提示,如何取消选中 HTML 框
【发布时间】:2018-05-04 02:29:28
【问题描述】:

目标:用户点击一个复选框,启动我的函数checkedAmPrompt。如果用户在提示上点击取消,它将取消选中他们选中的框。如果他们点击 OK,它将按计划继续并保持检查状态。目前它运行良好,只是我找不到取消选中该框的方法。

相关代码如下:

webpage.component.html -- 请注意,这是在一个表格中,其中有一个对应于每一行的复选框。

<td><input type="checkbox" class="form-control" id="checkedAm" (change)="checkedAmPrompt($event)"></td>

webpage.component.ts

// Prompt to confirm checkbox action
checkedAmPrompt(event: any) {
  const confirm = prompt('If you are certain that what you are doing is correct please type CONFIRM to continue.');

  // What to do if user selects cancel to pop-up, otherwise user confirms action
  if (confirm == null) {
    console.log('User hit cancel.');
    // ***** WANT TO PUT LOGIC TO UNCHECK HERE *****
  } else if (confirm.toLowerCase() === 'confirm') {
    // submit the changes if they type in confrim
    console.log('changes submitted!')
  } else {
    // if they misspell confirm let them know
    console.log('Incorrect spelling.');
  }
}

取消选中 HTML 框的逻辑将替换 // ***** WANT TO PUT LOGIC TO UNCHECK HERE *****

请注意,我没有使用 JavaScript,我的 HTML 文件仅包含 HTML/Angular,并且所有函数都由组件打字稿引用。我对一切都很陌生,所以如果这是一个糟糕的选择,请原谅。

【问题讨论】:

标签: angular


【解决方案1】:

只需使用document.getElementById('checkedAm').checked = false,这样行吗?

【讨论】:

  • 我收到以下错误:“类型 'HTMLElement' 上不存在属性 'checked'。”另请注意,我尝试使用 checked="true" 或 "false",但 true 和 false 都会导致复选框被选中。
【解决方案2】:

您可以使用ng-checked="(var bool or function that return bool)"

【讨论】:

  • 我试过这个。即使在输入中直接设置 ng-checked="true" ,它仍然会将该框显示为未选中。
  • 尝试使用 ng-change 而不是 (change) 并告诉我是否可以
  • 这也不起作用,这会导致我的函数无法执行。不过感谢您的尝试。
【解决方案3】:

弄清楚我需要做什么。

HTML:

[checked]="checkCheck()"

组件:

checkCheck () {
return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2017-08-29
    相关资源
    最近更新 更多