asp.net mvc 在 freetextbox 中赋值

发布于 2024-12-19 06:35:57 字数 180 浏览 4 评论 0原文

在我的 .cshtml 文件中,我有一个供用户输入的自由文本框。

@Html.EditorFor(model => model.FreeText)

如何在文本框中分配一个值,例如“在此处键入”,当用户单击文本框时,默认文本将消失?

谢谢

in my .cshtml file, i had a freetextbox for user to input.

@Html.EditorFor(model => model.FreeText)

How can i assign a value in the textbox such as "Type here", when user click the textbox, the default text will disappear ?

Thanks

Joe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灵芸 2024-12-26 06:35:57
Try this In View 
<input id="FreeText" name="FreeText" type="text" value="Type Here"/>
also include the java script given below
<script type="text/javascript">
   $(document).ready(function () {
    $('#FreeText').val('Type Here');
    $('#FreeText').focus(function () {
        var vlu = $(this).val();
        if (vlu == '' || vlu == 'Type Here') {
            $(this).val('');
        }
        else {
            var char = $(this).val();
            $(this).val(char);
        }

    });
$('#FreeText').blur(function () {
        var value = $(this).val();
        if (value == '' || value == 'Type Here') {
            $(this).val('Type Here');
        }
        else {
            var char = $(this).val();
            $(this).val(char);
        }
    });
    });
</script>
Try this In View 
<input id="FreeText" name="FreeText" type="text" value="Type Here"/>
also include the java script given below
<script type="text/javascript">
   $(document).ready(function () {
    $('#FreeText').val('Type Here');
    $('#FreeText').focus(function () {
        var vlu = $(this).val();
        if (vlu == '' || vlu == 'Type Here') {
            $(this).val('');
        }
        else {
            var char = $(this).val();
            $(this).val(char);
        }

    });
$('#FreeText').blur(function () {
        var value = $(this).val();
        if (value == '' || value == 'Type Here') {
            $(this).val('Type Here');
        }
        else {
            var char = $(this).val();
            $(this).val(char);
        }
    });
    });
</script>
动次打次papapa 2024-12-26 06:35:57

在 HTML5 中,您可以使用 占位符

<input type="text" placeholder="type here" />

http://jsfiddle.net/HMG4W/

var placeholder ="Type here";
$(":input").val(placeholder);
$(':input').bind({
  focus: function() {
    $(this).val(" ");
  },
  blur: function() {
    $(this).val(placeholder);
  }
});

http://jsfiddle.net/HMG4W/2/

in HTML5 you can use the placeholder

<input type="text" placeholder="type here" />

http://jsfiddle.net/HMG4W/

or

var placeholder ="Type here";
$(":input").val(placeholder);
$(':input').bind({
  focus: function() {
    $(this).val(" ");
  },
  blur: function() {
    $(this).val(placeholder);
  }
});

http://jsfiddle.net/HMG4W/2/

甜宝宝 2024-12-26 06:35:57

在 MVC5 中尝试遵循以下操作:

@Html.EditorFor(model => model.FreeText, new { htmlAttributes = new { @placeholder = "Type Here" } })

In MVC5 try to follow this:

@Html.EditorFor(model => model.FreeText, new { htmlAttributes = new { @placeholder = "Type Here" } })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文