简体   繁体   English

表格提交的JavaScript不起作用

[英]form submit javascript not working

my html is 我的HTML是

<form id="login" class="ui-body ui-body-a ui-corner-all" data-ajax="false" method="post" >
                <fieldset>
                    <div data-role="fieldcontain">
                        <label for="username">Usuario :</label>
                        <input type="text" value="" name="username" id="username"/>
                    </div>                                  
                    <div data-role="fieldcontain">                                      
                        <label for="password">Contraseña:</label>
                        <input type="password" value="" name="password" id="password"/> 
                    </div>
                    <input type="submit"  data-theme="b" name="submit" id="submit" value="Login">
                </fieldset>

           </form>  

and my javascript is 而我的JavaScript是

$("#login").submit(function(){
alert("OK");
}

but it isn't working. 但它不起作用。

i have check in http://jsfiddle.net/w2Sqs and it works, but in my web not! 我已经在http://jsfiddle.net/w2Sqs中签入了,并且可以,但是在我的网站上却没有!

Any idea? 任何想法?

thanks 谢谢

You are probably referencing your form too soon. 您可能过早引用了表单。 If you try and reference a DOM element before it is loaded, then it's going to fail to attach a listener to it. 如果您尝试在加载DOM元素之前引用它,那么它将无法向其附加侦听器。

Instead, either put the selector below your form: 而是将选择器放在表单下方:

   <body>
      <form id="login" method="post">
         <input type="text" name="user">
         <input type="submit" value="send">
      </form>
      <script>
         $("#login").submit(function() {
            alert("ok");
         });
      </script>
   </body>

Or make sure the DOM is loaded completely using the jQuery ready stuff (This code can go in the <head> section): 或者确保使用jQuery ready东西完全加载DOM(此代码可以放在<head>部分中):

     $(function() {
        $("#login").submit(function() {
           alert("ok");
        });
     });

尝试将事件添加到您的提交功能

$(#id).submit(function(event){});

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

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