简体   繁体   English

如何在 Blazor Server 应用程序的 Razor 页面中通过标记代码(单击输入字段时)清空文本输入字段?

[英]How can I empty a text input field via markup code (when I click the Input-field) in my Razor page of my Blazor Server app?

I have a text input field in my razor page, where the client should enter an IP address.我的剃须刀页面中有一个文本输入字段,客户端应在其中输入 IP 地址。 The initial text of the input field is "Enter IP adress here".输入字段的初始文本是“在此处输入 IP 地址”。 As soon as the client clicks into the input field, I want that the "Enter IP adress here" text dissapears, so that the client can write the IP address.一旦客户端单击输入字段,我希望“在此处输入 IP 地址”文本消失,以便客户端可以写入 IP 地址。 I have tried following code with "bind", but didn't work.我曾尝试使用“绑定”来跟踪代码,但没有奏效。 As you in the code, I have tried both with onclick and onselect events of the input field to delete the text.正如您在代码中一样,我已经尝试使用输入字段的 onclick 和 onselect 事件来删除文本。

@page "/connect2"

<h>How do you want to select your MAE?</h>
<br>
<select @onchange="func_ST">
<option value="">-- Select MAE from List --</option>
<option value="">-- Enter IP address --</option>
</select>

@if (TagService.selectedString_ST == "-- Enter IP address --")
{
<input type="text" @bind="@TagService.IP_Manuel"  onclick="func_IP_Set" onselect="func_IP_Set" >
}


@code {
async void func_ST(Microsoft.AspNetCore.Components.ChangeEventArgs e)
{
    if (TagService.selectedString_ST == "-- Enter IP address --")
    {
        TagService.IP_Manuel = "Enter IP";
        StateHasChanged();
    }

}
async void func_IP_Set()
{
    if(TagService.IP_Manuel=="Enter IP")
    {
        TagService.IP_Manuel = "";
        StateHasChanged();
    }
}
}

For simplicity, you might consider the placeholder attribute instead.为简单起见,您可以考虑使用占位符属性。 The placeholder text will appear when no value is set in the input and disappear once a value is entered.占位符文本将在输入中未设置值时出现,并在输入值后消失。

 <input type="text" placeholder="-- Enter IP address --">

A separate Label tag would be better than a placeholder within the input tag.单独的标签标签会比输入标签中的占位符更好。 This is because the label is then accessible if needed.这是因为标签可以在需要时访问。

If you put a label tag under your input, it'll work exactly how you want it..如果您在输入下放置标签标签,它将完全按照您的要求工作..

<input type="text" id=“text” />
<label for=“text”>IP Address</label>

暂无
暂无

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

相关问题 如何清除 Blazor 服务器应用程序中的文本类型输入字段? 清除相关字符串变量时字段未清除 - How can I clear a text type Input field in my Blazor server app? The field is not cleared when I clear the related string variable 如何在 Blazor Serv 中执行与 razor 页面输入字段的 onchange 事件相关的 function。 应用程序? (我必须在函数中使用输入值) - How can I perform a function related to a razor page input field's onchange event in Blazor Serv. App? (I have to use the input value in the function) 动态绑定输入字段 blazor - Dynamically binding input-field blazor 如何在下拉菜单中添加搜索输入字段? - How can i add a search input field in my dropdown? 统一按手机上的后退按钮后,如何在输入字段中保存输入的文本 - How can I save my typed text in the input field after pressing back button in mobile in unity 我的 Blazor 服务器应用程序剃须刀页面中有一个 iframe,其中定义了外部 URL。 如何使用按钮强制刷新(重新加载)此 url? - I have an iframe in my Blazor server app razor page in that an external URL is defined. How can I force to refresh (reload) this url using a button? 按下按钮时,Blazor 服务器应用程序中的输入字段未绑定 - Input field in Blazor server app is not binding when button pressed 当我的文本框在resourcedictionary中时,如何保存文本输入? - How can I save text input when my textbox is in resourcedictionary? Unity:为什么我不能自动选择输入字段? - Unity: Why can't I automatically select my input field? 如何从查询字符串的输入字段中发回文本? - How do I send back text from my input field in the query string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM