How to display watermark instead of value in textbox in ASP.Net MVC?

后端 未结 2 839
一整个雨季
一整个雨季 2021-01-20 09:29

In my view I have

<%= Html.TextBoxFor(m => m.Patient.Weight, new { title = \"Weight\" , style = \"width: 3em\", @class = \"fieldRequired watermark\" }         


        
2条回答
  •  半阙折子戏
    2021-01-20 10:29

    The term you're looking for is placeholder.

    There's an option with HTML 5 to specify a placeholder.

    
    @Html.TextBoxFor(m => m.Name, new { placeholder="Your Name..." })
    

    However, it's not supported on all browsers. So you're off to JavaScript land.

    Here's a popular option on NuGet - jQuery.placeholder. It'll read the HTML 5 attributes and handle fall-back for browsers that don't support it.

    Just add the script at this in the document onload. And some css classes, check their docs for further info.

    $('input, textarea').placeholder();
    

    Edit: placeholder is supported in all major modern browsers

提交回复
热议问题