最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - Button to clear textareatextboxes of content - Stack Overflow

matteradmin9PV0评论

would someone be able to help me with a button to clear /delete the content of all textareas on a form please?

I've already tried this approach:

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

but because the textareas are populated with data from the database upon load then it doesn't actually do anything (unless I want to re-set the form back to it's original state).

Can I do this with some code on the view? Or will I need to add some bits to the controller as well?

I'm using ASP.Net MVC Razor C# Microsoft Visual Web Developer 2010 Express.

Code for the View page is below:

@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>

would someone be able to help me with a button to clear /delete the content of all textareas on a form please?

I've already tried this approach:

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

but because the textareas are populated with data from the database upon load then it doesn't actually do anything (unless I want to re-set the form back to it's original state).

Can I do this with some code on the view? Or will I need to add some bits to the controller as well?

I'm using ASP.Net MVC Razor C# Microsoft Visual Web Developer 2010 Express.

Code for the View page is below:

@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>
Share Improve this question edited May 29, 2012 at 11:29 Darren 70.9k24 gold badges141 silver badges145 bronze badges asked Mar 23, 2012 at 15:41 HarryHarry 7455 gold badges16 silver badges27 bronze badges 1
  • have you considered a javascript approach? – Daniel A. White Commented Mar 23, 2012 at 15:44
Add a ment  | 

2 Answers 2

Reset to default 3

Since you're using JQuery you could assign all text areas a class and then do:

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

This would clear all text areas once the document has loaded.

You can use jQuery's .click event on your button:

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

comment list (0)

  1. No comments so far