清除文本区域/文本框内容的按钮

Button to clear textarea / textboxes of content

本文关键字:文本 按钮 区域 清除      更新时间:2023-09-26

有人能帮我用一个按钮来清除/删除表单上所有文本区域的内容吗?

我已经尝试过这种方法:

<input type="reset" value="restart" />

但因为文本区域在加载时填充了数据库中的数据,所以它实际上什么都不做(除非我想将表单重新设置回原始状态)。

我可以在视图中使用一些代码吗?或者我也需要给控制器添加一些位吗?

我使用的是ASP.Net MVC Razor C#Microsoft Visual Web Developer 2010学习版。

查看页面的代码如下:

@model DFAccountancy.Models.Data
@{
    ViewBag.Title = "Update";
}

<h2>Update</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">    </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"     type="text/javascript"></script>
<script type="text/javascript">
    $(function () { $("test").click(function () { $("textArea").val(""); }); });
</script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Data</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.para1)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.para1, new { cols = 75, @rows = 5 })
        @Html.ValidationMessageFor(model => model.para1)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.para2)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.para2, new { cols = 75, @rows = 5 })
        @Html.ValidationMessageFor(model => model.para2)
    </div>
    <p>
        <input type="submit" value="Update" />
    </p>
    <p>
        <input type="reset" value="Re-Start" />
    </p>
    <p>
        <input id="test" type="button" />
    </p>
</fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

由于使用JQuery,您可以为所有文本区域分配一个类,然后执行:

$(function () {
   $(".textArea").text('');
}

这将在加载文档后清除所有文本区域。

您可以使用jQuery的.点击按钮上的事件:

 $(function () {
   $("#YourButtonID").click(function(){
      $("textArea").val("");
   });
 });