简体   繁体   English

单击时清除闪电输入和闪电文本区域?

[英]Clear lightning-input and lightning-textarea on click?

Looking to reset the two text fields labeled 'Name' and 'Description' when the 'Cancel' button is clicked.当单击“取消”按钮时,希望重置标记为“名称”和“说明”的两个文本字段。 However, I'm getting the error ' [document.getElementById(...) is null]'.但是,我收到错误“[document.getElementById(...) is null]”。 After some research, I've learned this error appears when the JS is within the same file as the HTML, but this JS is in a separate file and is nested within the button click, so the fields already exist.经过一番研究,我了解到当 JS 与 HTML 位于同一文件中时会出现此错误,但此 JS 位于单独的文件中并嵌套在按钮单击中,因此字段已存在。 I've tried setting the value to a blank string, as well as resetting it using the .reset() method.我尝试将值设置为空字符串,并使用 .reset() 方法重置它。

The HTML: HTML:

<lightning-input label="Name" id="nameInput" name="nameInput"></lightning-input>
<lightning-textarea label="Description" id="descriptionInput" name="descriptionInput"></lightning-textarea>
<lightning-button label="Cancel" onclick={cancelStep}></lightning-button>

The JS: JS:

cancelStep(){
    document.getElementById('nameInput').value = '';
    document.getElementById('descriptionInput').value = '';
    //document.getElementById('nameInput').reset();
    //document.getElementById('descriptionInput').reset();
}

在 Lightning-input 中声明类并尝试下面的代码:

this.template.querySelector('nameInput').reset();

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

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