[LWC小知识] 标准lightning-input-field怎么取得变更值(onchange)
在lightning-record-edit-form
中,我们用lightning-input-field
自定义需要的字段。当我们需要得到用户的输入值(如Id,值等),我们可以在标签中加上onchange事件,然后在事件中抓取值。
html:
<lightning-input-field onchange={getValue} field-name="RF_SAMPLE_NAME__c"></lightning-input-field>
js:
getValue(event) {
const value = event.detail.value[0] // get input value
}
在lightning-input-field
中的detail值和record-edit-form
中的不同,需要在value[0]
中获取。
而在record-edit-form
中,我们如果需要得到onsuccess
中返回的Id值,则需要通过event.detail.Id
获取。
这些都是文档中有的内容,不过使用场景很多,最好记下来。