English 中文(简体)
如何在不向服务器发出要求的情况下使用java字母的形式。
原标题:how to use javascript in form runat=server without sending request to the server?

我的主页在“服务器”等形式内:

<body>
    <form id="Form1" runat="server">
         <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
    </form>
</body>

在我的一页中,我增加了一个文本区域,一个纽州(html Control,而不是ASPs)和一些javascript代码,如:

<textarea id="ContentTextArea" class="ContentTextArea" rows="2" cols="20"></textarea>
<button id="ClearButton" class="Button2" onclick="ClearButtonClick();">CLEAR</button>

    <script type="text/javascript">
        function ClearButtonClick() {
            document.getElementById("ContentTextArea").value = "";
        }
    </script>

我希望 j印法(正在清理正文体)在客户不向服务器发出要求的情况下点击“clear”纽扣时运行,但因为我有<form id=“Form1”prat=“server”>在纽特纳总页将数据提交服务器并重载。

我必须拥有<代码><form id=“Form1”prat=“server”>载于我的主页,但我还要使用 JavaSctipt在客户方面操作的手法。

成就

EDIT:

我的主页是:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="Form1" runat="server">
        <div class="header">
             <div class="clear hideSkiplink">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                        <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
                        <asp:MenuItem NavigateUrl="~/ContactUs.aspx" Text="Contact Us"/>
                    </Items>
                </asp:Menu>
            </div>

            <div class="loginDisplay">
                <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                    <AnonymousTemplate>
                        [ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
                        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
                    </LoggedInTemplate>
                </asp:LoginView>
            </div>
        </div>
        <div class="clear">
        </div>

        <div class="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>

        <div class="footer">
            <p>GH Electronics © 2012. All Rights Reserved.</p>
        </div>

    </form>
    <input type="button" onclick="window.location =  /WareHouse/ContactUs.aspx ;" value="avi"/>
    <script type="text/javascript">

           function ClearButtonClick() {
            document.getElementById("ContentTextArea").value = "";
            return false;
        }
    </script>
</body>


</html>

我与我们的联系形式是:

<img alt="Contact Us" src="Images/contact_us.gif" style="float:left;"/>

<p class="contactUs" runat="server">CONTACT US</p>
<label>Full Name:</label>
<asp:TextBox class="FullNameTextBox" runat="server" />
<br /> <br />
<label>Phone Number:</label>
<asp:TextBox class="PhoneNumberTextBox" runat="server" />
<br /> <br />
<label>Email:</label>
<asp:TextBox class="EmailTextBox" runat="server" />
<br /> <br />
<label>Subject:</label>
<asp:TextBox class="SubjectTextBox" runat="server" />
<br /> <br />
<label>Content:</label>
<textarea id="ContentTextArea" class="ContentTextArea" rows="2" cols="20"></textarea>
<br /> <br />
<asp:Button ID="SendButton" class="SendButton" Text="SEND" runat="server" />


<button id="ClearButton" class="Button2" onclick="ClearButtonClick();">CLEAR</button>

<script type="text/javascript">

      function ClearButtonClick() {
       document.getElementById("ContentTextArea").value = "";
       return false;
   }

最佳回答

想这样做:

<button id="ClearButton" class="Button2" onclick="javascript:ClearButtonClick();return false;">CLEAR</button>
问题回答

这样做。 你必须在两个地方作出改变。

  1. 添加在 on=“return ClearButtonClick()”上的回报;“活动登记如下。

      <button id="ClearButton" class="Button2" onclick="return ClearButtonClick();">CLEAR</button>
    
  2. Return false from javascript function to stop it from send request to server Put this in end of page or between head tag.

    <script type="text/javascript">
        function ClearButtonClick() {
            document.getElementById("ContentTextArea").value = "";
            return false;
        }
    </script>
    




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签