簡體   English   中英

用戶開始填寫表格時清除字段

[英]Clear a field when the user starts filling a form

我有一個表單的視圖:

<form asp-controller="UrlP" asp-action="RegisterInput" method="post"> 
Url:  <input asp-for="Url" />
<br />
<button type="submit">Go!</button>

同樣,從上次提交的結果來看:

@if (!string.IsNullOrEmpty(Model.Result))
{
    <div class="result">The result is: @Model.Result</div>
}

用戶開始輸入表格后,如何使上面的div(“結果”類)消失?

為了提供一些背景信息,該應用程序是一個頁面,您在其中提供網址,並在下面獲得結果,我想在輸入新網址后立即清除結果。

這是一種方法。

 window.onload = function () { // attach onkeypress event handler to all text inputs document.querySelectorAll('form [type=text]').forEach(function (input) { input.onkeypress = hideResult; }); }; function hideResult () { var result = document.querySelector('.result'); // remove result if it exist if (result) { result.parentNode.removeChild(result); } } 
 <div class="result">The result is: @Model.Result</div> <form> <input type="text" value=""><br> <input type="text" value=""> </form> 

由於您使用的是ASP.net Core,因此可以從操作和模型狀態進行處理

因此,發布后,只需清除您的ModelState並返回如下視圖:

ModelState.Clear();
return View(<some View>);

暫無
暫無

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

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