简体   繁体   English

无法使用javascript从输入表单获取值

[英]Unable to get values from input form using javascript

Html input: HTML输入:

<input type="text" id="email" name="email" class="inputbox" placeholder="Email" required="required" />
<input type="password" id="pass" name="pass" class="inputbox" placeholder="Password" required="required" />
<button class="btn btn-red" onclick="logincheck();">LOG IN</button>

Javascripts: JavaScript的:

function logincheck(){
     var e =  document.getElementById("email").value;
      p = $("#pass").val();
        console.log(e);

        if($("#email").val()==""||$("#pass").val()=="")
           alert("Email and password can't be empty");  
        else { 
            $.ajax({
                    url : "http://localhost/dsbd/loginprocess.php",
                    type: "POST",
                    data : { 
                        email : e,
                        password: p
                        },
                        dataType: 'text',
                        success: function (data) { 
                            alert(data);

                        }
                });
            }
    }

e and p variables are returning empty string (while printing on console). ep变量返回空字符串(在控制台上打印时)。 Added the code on jsfiddle Please help me to find out the problem. jsfiddle上添加了代码,请帮助我找出问题所在。

In my opionion you just have to change your script (that is half javascript and half jquery with also some errors) in order to have a more clear code... 在我的选择中,您只需要更改脚本(即一半的javascript和一半的jquery,还带有一些错误),以便获得更清晰的代码...

Here you have a working fiddle for jquery: 在这里,您可以使用jQuery的小提琴:

 $(document).ready(function(){ $('#my-login-button').click(function() { if($('#email').val().length == 0 || $("#pass").val().length == 0) { alert("Email and password can't be empty"); } else { $.ajax({ url : "http://localhost/dsbd/loginprocess.php", type: "POST", data : { email : $('#email').val(), password: $("#pass").val() }, dataType: 'text', success: function (data) { alert(data); } }); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="email" name="email" class="inputbox" placeholder="Email" required="required" /> <input type="password" id="pass" name="pass" class="inputbox" placeholder="Password" required="required" /> <button id="my-login-button" class="btn btn-red">LOG IN</button> 

And here you have a javascript solution: 在这里,您有一个javascript解决方案:

 function logincheck() { var e = document.getElementById("email").value; var p = document.getElementById("pass").value; console.log(e); if(e.length == 0 && p.length == 0) { alert("Email and password can't be empty"); } else { $.ajax({ url : "http://localhost/dsbd/loginprocess.php", type: "POST", data : { email : e, password: p }, dataType: 'text', success: function (data) { alert(data); } }); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="email" name="email" class="inputbox" placeholder="Email" required="required" /> <input type="password" id="pass" name="pass" class="inputbox" placeholder="Password" required="required" /> <button id="my-login-button" onclick="logincheck()" class="btn btn-red">LOG IN</button> 

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

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