[英]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.