简体   繁体   English

使用Javascript生成表单时不会提交

[英]Form won't submit when generated with Javascript

I'm generating a login form in javascript. 我正在javascript中生成一个登录表单。 The form displays perfectly and the HTML looks fine in Google Dev Tools but the form won't submit when I click the submit button. 表单显示完美,HTML开发工具中的HTML看起来很好,但单击提交按钮时表单将不会提交。

Heres's the generated form code: Heres是生成的表单代码:

var loginBox = '<div id="loginBox">';
loginBox    += '<h1>Login</h1>';
loginBox    += 'Please use the form to login below<br /><br />';
loginBox    += '<form action="home.php" method="post">';
loginBox    += '<div class="input">';
loginBox    += '<label for="username">Username</label>';
loginBox    += '<input type="text" name="username" />';
loginBox    += '</div>';
loginBox    += '<div class="input">';
loginBox    += '<label for="userpass">Password</label>';
loginBox    += '<input type="password" name="password" />';
loginBox    += '</div>';
loginBox    += '<div class="clear"></div>';
loginBox    += '<div class="input">';
loginBox    += '<input type="submit" class="submit button" value="Submit" />';
loginBox    += '</div>';
loginBox    += '</form>';
loginBox    += '</div>';

Any ideas on why this wouldn't work? 为什么这不起作用的任何想法? I know I could probably write some Javascript to submit the form when the submit button is pressed but I don't see why that should be necessary. 我知道我可能会在按下提交按钮时写一些Javascript来提交表单,但我不明白为什么这是必要的。 Thanks 谢谢

The bigger question is why are you creating a login box with Javascript? 更大的问题是为什么要使用Javascript创建登录框?

I think that it is cleaner to refactor it and have the form live inside a hidden div, and use JavaScript to show/hide it as required. 我认为重构它并使表单存在于隐藏的div中更清晰,并使用JavaScript根据需要显示/隐藏它。 This would get around some brower's issues with dynamically adding a form using element.innerHTML. 通过使用element.innerHTML动态添加表单,这将解决一些浏览器的问题。

If that is not an option then you may have to change your JS to add it using the DOM. 如果这不是一个选项,那么您可能必须更改您的JS以使用DOM添加它。 This is quite verbose and not very easy to read. 这非常冗长,不易阅读。

I added it to this page and it seemed to work. 我把它添加到这个页面,它似乎工作。 Do you have any client validation scripts that might intercept the form submit event? 您是否有可能拦截表单提交事件的客户端验证脚本?

My code: 我的代码:

$(loginBox).appendTo("body");

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

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