简体   繁体   English

表单不在按钮上通过javascript提交

[英]Form is not submitting via javascript on a button

My website has been created with a CSS/HTML frame work that has been integrated into an ASP.NET website. 我的网站是使用CSS / HTML框架创建的,该框架已集成到ASP.NET网站中。

Inside of a ContentPlaceHolder, I have a simple login form. 在ContentPlaceHolder内部,我有一个简单的登录表单。 The catch is that I am using the onclick event of an image to submit the form. 问题是我正在使用图像的onclick事件提交表单。 It normally works pretty straight forward, but this time I am having an issue. 通常它很简单,但是这次我遇到了问题。

<div id="login">
    <form action="index.aspx" method="post" id="nothingForm" name="nothingForm">
    </form>
    <form action="https://google.com.au" method="post" id="loginform" name="loginform">
        <input type="text" name="uname" value="username" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;"/>
        <input type="password" name="pword" value="password" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;"/>
    </form>
    <br /><img src="images/login.gif" alt="" onclick="document['loginform'].submit()" />    </div>

I have to stick another form before the real form to make onclick="document['loginform'].submit()" actually work. 为了使onclick =“ document ['loginform']。submit()”真正起作用,我必须粘贴另一种形式。 This should not be the case, of course, but I was unable to find any solutions to it on the web. 当然,情况并非如此,但是我无法在网络上找到任何解决方案。

Has anyone else come across this before? 有人遇到过吗?

Did you try out document.getElementById("loginform").submit() ? 您是否尝试了document.getElementById("loginform").submit() I would think this is better, since I have never seen anyone access elements on the document itself like that before. 我认为这样做会更好,因为我从未见过像以前那样可以访问文档本身上的元素。 May be some kind of strange side effect. 可能是某种奇怪的副作用。

Your problem is that the page already has a form around all the code. 您的问题是该页面已经具有围绕所有代码的表单。 Forms can not be nested, so your first form tag will be ignored, and it's end tag will end the outer form. 表单不能嵌套,因此您的第一个表单标签将被忽略,而它的end标签将终止外部表单。

You have to place your form outside the already existing form. 您必须将表单放置在现有表单之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM