我正在使用带有信用卡号的表单构建示例组件。我试图将oninput事件添加到InputText组件,一旦附加该组件便无法正常运行。没有事件,表单将正常运行。不管事件提供的方法做什么,它始终无法正常运行。
代码:
<microsoft.AspNetCore.Components.Forms.EditForm Model="@card" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div>
<div>
<p>
<label for="ccNumberId">Debit or Credit Card Number</label><br />
<InputText id="ccNumberId" @bind-Value="card.Number"
@oninput="@(e => FormatCreditCardNumber())" /><br />
@numberFormat
</p>
<p>
<label for="ccYearId">Expiration Year</label><br />
<InputText id="ccYearId" @bind-Value="card.ExpYear" />
</p>
<p>
<label for="ccMonthId">Expiration Month</label><br />
<InputText id="ccMonthId" @bind-Value="card.ExpMonth" />
</p>
<p>
<label for="ccCvcId">CVC</label><br />
<InputText id="ccCvcId" @bind-Value="card.Cvc" />
</p>
</div>
</div>
<button type="submit">Submit</button>
</microsoft.AspNetCore.Components.Forms.EditForm>
@code {
string numberFormat;
private StripeChargeModel card = new StripeChargeModel();
protected override void OnInitialized()
{
}
private void HandleValidSubmit()
{
JSRuntime.InvokeVoidAsync("showOnToast","#paymentToast");
}
private void FormatCreditCardNumber()
{
if (card.Number.Length == 4)
{
numberFormat = String.Empty;
numberFormat = card.Number + "-";
}
}
}