简体   繁体   English

获取 jQuery function 以触发 HTML 按钮 OnClick

[英]Get jQuery function to trigger on HTML Button OnClick

I have the following HTML/JQuery:我有以下 HTML/JQuery:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<script src="jQuery.js"></script>
<script>
   $(document).ready(function(){                     
      $(function(){
         $("#sb").click(function(e){
            e.preventDefault();
            getmessage = "Yes";
            getmessage = encodeURIComponent(getmessage);//url encodes data
            $.ajax({
               type: "POST",
               url: "get_login.php",
               data: {'getmessage': getmessage},
               dataType: "json",
               success: function(data) {
                  $("#message_ajax").html("<div class='successMessage'>" + data.message +"</div>");
           }
           });
        });
      });
    });
</script>
</head>

<body>
<h1>Login</h1>
<p>Please enter your e-mail and password</p>
<form name = "loginform" action = "http://dev.speechlink.co.uk/David/get_login.php" method = "post">
<p>E-mail<input name = "username" type="text" size="25"/><label id ="emailStatus"></label></p>

<p>Password<input name = "password" type="text" size="25"/><label id ="passwordStatus"></label></p>

<p><input type="hidden" name="usertype" value = "SupportStaff" /></p>
<p>><input type ="button" id = "sb"value="Create Account"/></p>

</form>
<div id = "message_ajax"></div>
</body>
</html>

I cannot get the event handler (within the jQuery) to trigger upon the user pressing the 'Create Account' button...我无法让事件处理程序(在 jQuery 中)在用户按下“创建帐户”按钮时触发......

I see three issues off-the-bat:我立即看到三个问题:

  1. You're calling ready twice (when you pass a function into $() , it's just like passing a function into $(document).ready() ).您调用了两次ready (当您将 function 传递给$()时,就像将 function 传递给$(document).ready()一样)。 This is largely harmless, but completely unnecessary.这在很大程度上是无害的,但完全没有必要。

  2. Your attribute for the id on the button is rammed up against the next;您在按钮上的id属性被撞到下一个; it's possible the browser is ignoring it:浏览器可能会忽略它:

     <input type ="button" id = "sb"value="Create Account"/> <!-- ^^^^^^^^^ -->

    (It's fine to have spaces around the = if you want, but having the "sb" rammed up into value may not work.) (如果需要,可以在=周围留有空格,但将"sb"value可能行不通。)

  3. (Possibly off-topic, possibly not): You seem (from the quoted code) to be falling prey to The Horror of Implicit Globals . (可能偏离主题,可能不是):您似乎(从引用的代码中)成为隐式全局恐怖的牺牲品。 You should probably declare your getmessage variable.您可能应该声明您的getmessage变量。

So:所以:

<script>
   $(document).ready(function(){           
     // Removed the unnecessary `$(function() { ...` here and the matching closing bits at the end
     $("#sb").click(function(e){
        e.preventDefault();
        var getmessage = "Yes"; // <== Added `var`
        getmessage = encodeURIComponent(getmessage);//url encodes data
        $.ajax({
           type: "POST",
           url: "get_login.php",
           data: {'getmessage': getmessage},
           dataType: "json",
           success: function(data) {
              $("#message_ajax").html("<div class='successMessage'>" + data.message +"</div>");
       }
       });
    });
    });
</script>

and

<input type="button" id="sb" value="Create Account"/>

You html is not good:你html不好:

<p>><input type ="button" id = "sb" value="Create Account"/></p>

should be应该

<p><input type ="button" id="sb" value="Create Account"/></p>

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

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