Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么当我的函数中有警报但没有';当我不知道的时候_Javascript_Html_Partial Page Refresh - Fatal编程技术网

Javascript 为什么当我的函数中有警报但没有';当我不知道的时候

Javascript 为什么当我的函数中有警报但没有';当我不知道的时候,javascript,html,partial-page-refresh,Javascript,Html,Partial Page Refresh,我有一个html页面,叫做index.html,它看起来像: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <title>sometitle</title> <link rel="stylesheet" href="style.css" /> </h

我有一个html页面,叫做index.html,它看起来像:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>sometitle</title>
<link rel="stylesheet" href="style.css" />  

</head>
<body>
<div id="Screen">
<section class="loginform cf">
    <form action="#" method="post" id="loginwindow">
        <ul>
            <li>
                <label for="usermail">Email</label>
                <input type="email" name="useremail" placeholder="youremail@email.com" required>
            </li>
            <li>
                <label for"password">Password</label>
                <input type="password" name="password" placeholder="password" required>
            </li>
            <li>
                <input type="submit" value="Check In">
            </li>
        </ul>
    </form>
</section>  

<script src="js/index.js"></script>
</div>
</body>
</html>
第二个html文件名为LoopIt_DashBoard.html,如下所示:

window.onload = function () {
var loginForm = document.getElementById('loginwindow');  
if ( loginwindow ) {
    loginwindow.onsubmit = function () {
        var useremail = document.getElementById('useremail');
        var password = document.getElementById('password');

        // Make sure javascript found the nodes:
        if (!useremail || !password ) {     
            load("LoopIt_DashBoard.html");
            return "success";
        }

    }
}
}

function load(url)
{

$("#Screen").load(url, function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
test();

}

function test()
{

alert("test");

}
function test(string)
{

alert(string);

}
<div id="Screen">
<section  class="loginform cf">
    <form action="#" method="post" id="loginwindow">
        <ul>
            <li>
                <input type="submit" value="Check In">
            </li>
        </ul>
    </form>
</section>  
</div>


那么这个函数就不起作用了,页面也不会改变。如何在不使用警报的情况下更新视图。

如果这是原始代码,则它不应该工作,因为您只设置了Name属性:

HTML

除了一些不干净的代码,它看起来很好

我还会稍微清理一下代码,例如:

window.onload = function () {
var loginForm = $('#loginwindow')[0];  
if ( loginwindow ) {
    loginwindow.onsubmit = function () {
        var useremail = $('#useremail')[0];
        var password = $('password')[0];
        // Make sure javascript found the nodes:
        if ( useremail && password ) {     
            load("LoopIt_DashBoard.html");
            return "success";
        }

     }
   }
}

...
重要 这些函数相互重写,因为它们具有相同的名称

function test()
{
    alert("test");
}

function test(string)
{
    alert(string);
}
提示如果这不是原始代码,请检查/计数卷曲和正常的分支。这可以解释这种奇怪的现象。

你也在打字。。。
它应该在if语句中是loginFrom,而不是loginWindow。


警报
将暂停JavaScript函数的执行,允许事件处理触发的任何其他JavaScript接管,导致当前事件处理程序的上下文丢失

如果没有此警报,您的
load()
将被遗忘,因为您的
onsubmit
事件处理程序将正常返回,并允许发布表单数据,本质上刷新页面,尽管操作是
“#”
,因为这是一个POST。因此,您的视图可能正在更新,但是文档也在重新加载,所以看起来不是这样


如果您自己处理表单,则需要取消表单的
onsubmit()
。一种方法是返回
false
而不是
“success”

尝试使用return true或return false(根据您的要求)而不是test(),这可能是不相关的,但在onload中,您设置了loginForm,然后测试loginwindow。另外,“test”的两个定义并不像我想的那样有效。第二个重新定义了第一个(写在上面)。谢谢,我意识到javascript在发布这篇文章后不久没有覆盖,这是我第一次真正尝试html和java脚本。Great Catch没有注意到缺少的
return false
...

<input type="email" name="useremail" placeholder="youremail@email.com" required>

...
...
var useremail = document.getElementById('useremail'); //but you just set the Name Attribute
...
window.onload = function () {
var loginForm = $('#loginwindow')[0];  
if ( loginwindow ) {
    loginwindow.onsubmit = function () {
        var useremail = $('#useremail')[0];
        var password = $('password')[0];
        // Make sure javascript found the nodes:
        if ( useremail && password ) {     
            load("LoopIt_DashBoard.html");
            return "success";
        }

     }
   }
}

...
function test()
{
    alert("test");
}

function test(string)
{
    alert(string);
}
var loginForm = document.getElementById('loginwindow');  
      if ( loginForm )