簡體   English   中英

onsubmit 事件不適用於 Lightning 記錄編輯表單

[英]onsubmit event not working for Lightning Record Edit Form

According to the Lightning Web Component documentation, https://developer.salesforce.com/docs/component-library/bundle/lightning-record-edit-form/documentation , you should be able to create an onsubmit event and prevent the form from正在提交。

這似乎不起作用。 這是一個重現問題的簡單組件。

<template>
  <lightning-record-edit-form object-api-name="Account" onsubmit={handleSubmit}>
    <lightning-input-field field-name="Name" value={value}> </lightning-input-field>
  </lightning-record-edit-form>
</template>
import { LightningElement } from 'lwc';

export default class FormSubmit extends LightningElement {
  value = 'Put cursor here, hit enter';

  handleSubmit(event) {
    event.preventDefault();
    console.log('This is not happening!!!');
    return false;
  }
}

將您的 cursor 放在該字段中,然后按 Enter 鍵。 頁面將刷新,handleSubmit function 永遠不會被調用。

我覺得我在這里發瘋了……這是一個錯誤嗎? 文檔有錯嗎? 我錯過了什么明顯的東西嗎? 請幫忙!!!

這是修復,確保表單包含一個按鈕! 而已!

<template>
  <lightning-record-edit-form object-api-name="Account" onsubmit={handleSubmit}>
    <lightning-input-field field-name="Name" value={value}> </lightning-input-field>
    <lightning-button variant="brand" type="submit" label="Save"> </lightning-button>
  </lightning-record-edit-form>
</template>

當然,在花費數小時調試此問題后,我在發布此問題 30 分鍾后找到了解決方案。

我沒有按鈕的原因是我將此表單用於內聯編輯功能,並且不需要按鈕,因為我正在單獨處理保存而無需用戶單擊提交。 我可以簡單地用一些 CSS 隱藏按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM