简体   繁体   中英

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. 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. here is my code 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:

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

nevermind is correct - no problem with that code.

Here's the JSFiddle to prove it: 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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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