繁体   English   中英

event.preventDefault似乎不起作用

[英]event.preventDefault doesn't seem to work

所以我试图阻止表单刷新页面,这是我正在使用的代码:

$("#getName").submit(function(refresh) {
    refresh.preventDefault();
    $("#p22").fadeOut(50);
    $("#p23").fadeIn(800);
    document.getElementById("p23").innerHTML = "Oh " + userName + ", alright i wont forget that!";

})

我似乎无法正常工作...。

id = getName-是表单ID

您的问题类似于: https : //stackoverflow.com/a/6462306/986160

尝试这个:

$("#getName").submit(function(refresh) {
    $("#p22").fadeOut(50);
    $("#p23").fadeIn(800);
    $("#p23").html("Oh " + userName + ", alright i wont forget that!");
    return false;
})

通过返回false就像refresh.preventDefault()和refresh.stopPropagation()一样;)在此处查看详细信息: https : //stackoverflow.com/a/1357151/986160

我得到了jQuery文档的此示例代码,它似乎可以正常工作(它发出警报,但随后未链接到destination.html 。您的代码中的所有内容看起来都不错,可能与您要查找的有所不同。

http://api.jquery.com/submit/

 $( "#target" ).submit(function( event ) { alert( "Handler for .submit() called." ); event.preventDefault(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <form id="target" action="destination.html"> <input type="text" value="Hello there"> <input type="submit" value="Go"> </form> <div id="other"> Trigger the handler </div> 

返回false不会验证您的表单,并且您的页面也不会刷新。

$("#getName").submit(function(refresh) {
    refresh.preventDefault();
    $("#p22").fadeOut(50);
    $("#p23").fadeIn(800);
    document.getElementById("p23").innerHTML = "Oh " + userName + ", alright i wont forget that!";
    return false;
})

暂无
暂无

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

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