简体   繁体   English

使用jQuery提交表单

[英]submitting form using jquery

got a problem and cant find the solution. 有一个问题,找不到解决方案。 I am writing a chat. 我在写聊天。 When a new user opens my site (a new session) a div popes out and the user is asked to fill in his name. 当新用户打开我的网站(新会话)时,会弹出一个div,并要求该用户填写其姓名。 The form works fine when I use an input submit. 当我使用输入提交时,表单工作正常。 I want it to work without the submit button, I want it to work when i press a div. 我希望它在没有提交按钮的情况下工作,当我按div时,我希望它工作。 here is my code html: 这是我的代码html:

<form name="form" id="form" action="index.html" method="post">
<span id="nspan">First name:</span> <input type="text" id="firstname"  name="name">
<div name="enter" id="enter">Submit</div>
</form>

the jquery: jQuery的:

$(document).ready(function(){
$("#enter").click(function () {
$("#form").submit();
});
});

nevermind is correct - no problem with that code. nevermind是正确的-该代码没有问题。

Here's the JSFiddle to prove it: http://jsfiddle.net/8Xk7z/ 这是JSFiddle来证明的: http : //jsfiddle.net/8Xk7z/

Maybe you problem is that the id "form" is to general a name, and you already used it for another form. 也许您的问题是id“表单”是一个通用名称,而您已经将它用于另一个表单。

Another thing, why not use a button or a link? 另一件事,为什么不使用按钮或链接? You can style it like you want. 您可以根据需要设置样式。 Be careful when you use thing for what they are not suppose to be used for, it my give unexpected side effects. 当您将它们用于不该使用的东西时要小心,这会给我带来意想不到的副作用。 In your case, you may only be able to login to you chat using a mouse, that would exclude blind people. 在您的情况下,您可能只能使用鼠标登录聊天,这将排除盲人。 You would also not be able to use the tabulater to get to the login "button". 您也将无法使用制表器进入登录“按钮”。 And last, if you are blind and uses a screen reader your would actually not know that there is at login "button", as the reader would not recognize the div as something you can click. 最后,如果您是盲人并且使用屏幕阅读器,您实际上将不知道登录“按钮”处,因为阅读器不会将div识别为您可以单击的东西。

I would recomend using the button-tag like this 我建议像这样使用按钮标签

<button id="enter">Submit</button>

Or the a-tag like this 或像这样的标签

<a href id="enter">Submit</a>

If you don't like the predefined styling of them you may always override the styling. 如果您不喜欢它们的预定义样式,则可以始终覆盖样式。

try to define jquery at top of the page 尝试在页面顶部定义jquery

<script src="https://code.jquery.com/jquery-3.2.1.js"></script>

Then put your script at next. 然后将您的脚本放在下一步。

still issue. 仍然发出。

Please check your other function on same page works fine or not. 请检查同一页面上的其他功能是否正常。

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

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