简体   繁体   English

在IE和Firefox中使用Javascript表单onSubmit无法正常工作

[英]Form onSubmit with Javascript not Working Correctly in IE and Firefox

OK, I submitted a question a little while ago pertaining to an animated loading GIF not displaying while an image is uploading ( PHP ). 好的,我刚才提交了一个问题,关于动画加载GIF在图像上传时没有显示(PHP)。 Now, I've managed to get the GIF to display while an image is uploading, but in IE and Firefox the loading GIF never goes away ( and / because ? ) the form never submits. 现在,我已经设法在图像上传时显示GIF,但在IE和Firefox中加载GIF永远不会消失(和/因为?)表单永远不会提交。 In Chrome, the GIF goes away and the form submits. 在Chrome中,GIF消失,表单提交。 The following is what I have doing it.. 以下就是我的做法..

<!-- Validates form -->
<script type="text/javascript" src="jquery-1.8.0.js"></script>
<script>
function validationloading(){
    // post box blank
    var x=document.forms["livefeed"]["message"].value;
    if (!$.trim($("#styled").val())){
        document.livefeed.message.focus();
        document.livefeed.message.style.border="solid 1px red";
        alert("Cannot post a blank message.");
        return false;
    }

    document.write("<table cellpadding='0' cellspacing='0' border='0' align='center' width='100%' height='130'><tr><td align='center' valign='center'><img src='images/ajax_loader.gif' width='50' height='50'></td></tr></table>");
    return true
    $("#myform").submit();
}
</script>

The following snip is what calls it from the form.. 以下剪辑是从表单中调用它的..

onSubmit="javascript: validationloading()" 

I've tried shuffling the return true value around and it changes the results but not in the way I need. 我试过改变返回的真值,它改变了结果但不是我需要的方式。 It'll submit in IE and Firefox but not display the loading GIF. 它将在IE和Firefox中提交但不显示加载GIF。 I know my code is a pile of slop, but that's why I'm here. 我知道我的代码是一堆污水,但这就是我在这里的原因。

The following works 100% across the three browsers.. I appreciate all your help, guys ! 以下在三个浏览器中100%工作..我感谢您的帮助,伙计们!

<!-- validates form and displays loading gif -->
<script type="text/javascript" src="jquery-1.8.0.js"></script>
<script>
function validationloading(){
    // post box blank
    var x=document.forms["livefeed"]["message"].value;
    if (!$.trim($("#styled").val())){
        document.livefeed.message.focus();
        document.livefeed.message.style.border="solid 1px red";
        alert("Cannot post a blank message.");
        return false;
    }
    //

    document.livefeed.submit(); 
    document.getElementById("gif_block").style.display="block"; //gif_block is the id of the div outside the image
    document.getElementById('livefeed').style.display ='none';
    return true;

}
</script>

and the GIF div is this.. 和GIF div是这个..

<div id="gif_block" style="display:none" align="center" ><p><image src="images/ajax_loader.gif" id="loader-img" width="50" height-"50" /></p></div>

There are a couple of issues there: 那里有几个问题:

  1. If you want validationloading to prevent the submission via its return value, you need to add return to that: 如果您希望validationloading加载通过其返回值来阻止提交,则需要添加return值:

     onsubmit="return validationloading()" 

    That's because under-the-covers, the browser converts the above to a handler function that looks something like this: 这是因为在封面下,浏览器将上面的内容转换为处理程序函数,如下所示:

     function(event) { // ...your code here... } 

    ...and uses the return value of that function. ...并使用函数的返回值。

  2. If you use document.write in a loaded page, the entire page content is wiped out and replaced with what you output in document.write . 如果在加载的页面中使用document.write ,则整个页面内容将被删除并替换document.write输出的document.write I wouldn't be surprised if that interfered with the form submission. 如果这干扰了表格提交,我不会感到惊讶。 document.write has basically no place in web apps and hasn't for the last 12 years or so. document.write在Web应用程序中基本没有位置,并且在过去12年左右没有。 :-) Use the DOM objects and methods to add elements to the page, and/or to change their styling so you hide some and reveal others. :-)使用DOM对象和方法向页面添加元素,和/或更改其样式,以便隐藏一些并显示其他内容。 (A bit of searching on your favorite search engine should give you plenty to work with.) (在你最喜欢的搜索引擎上搜索一下就可以给你很多工作。)

  3. You have code that can never be reached: The submit call after the return true . 您有永远无法访问的代码: return true后的submit调用。

     return true $("#myform").submit(); // <== This code will never run 

    return exits the function, so the code after it will never run. return退出该函数,因此它之后的代码永远不会运行。

Other notes (which are almost certainly not the problem): 其他笔记(几乎肯定不是问题):

  • With onxyz handler attributes, you don't use the javascript: pseudo-protocol. 使用onxyz处理程序属性,您不使用javascript:伪协议。 Their content is already JavaScript. 他们的内容已经是JavaScript。 You only use javascript: where a URL is expected, as with the href on a link. 您只使用javascript:其中包含URL,与链接上的href It doesn't cause an error, though, usually, because it looks like a labelled statement to the JavaScript engine. 但是,它通常不会导致错误,因为它看起来像是JavaScript引擎的标签语句
  • The proper attribute name is in all lower case. 适当的属性名称全部为小写。 It only matters if you're doing XHTML, though, so it probably doesn't matter for you. 但是,只有你正在做XHTML才有意义,所以它对你来说无关紧要。

There are a few things wrong with your code: 您的代码有一些问题:

  • javascript: is only meaningful in href or action attributes - basically wherever you might find a URL. javascript:仅在hrefaction属性中有意义 - 基本上只要你找到一个URL。 It is a scheme to indicate JavaScript code to be run. 这是一个表示要运行的JavaScript代码的方案。 In an event handler or a <script> tag, it's just a label that you might use as part of a loop. 在事件处理程序或<script>标记中,它只是一个标签,您可以将其用作循环的一部分。
  • return always ends execution of the current block, so it makes absolutely no sense to return true and then try to $("#myform").submit(); return总是结束当前块的执行,所以return true然后尝试$("#myform").submit();完全没有意义$("#myform").submit(); .
  • In fact, that $("#myform").submit() is potentially dangerous in that it may well cause an infinite loop: by submitting the form within its own onSubmit handler, you fire the handler again and try to submit the form anew, and so on. 事实上, $("#myform").submit()有潜在危险,因为它可能会导致无限循环:通过在自己的onSubmit处理程序中提交表单,您再次触发处理程序并尝试重新提交表单, 等等。
  • document.write() will overwrite the entire page if it's not being called while the page is loading. 如果在加载页面时没有调用它, document.write()将覆盖整个页面。 While in this case that's not really too much of an issue, I'm fairly sure it's not what you were intending to do. 虽然在这种情况下,问题并不是太多,但我很确定这不是你打算做的事情。
  • document.forms is obsolete, you should instead use proper DOM methods like getElementById . document.forms已经过时,您应该使用正确的DOM方法,如getElementById However in this case you could just pass this as an argument to validationloading and it will refer to the form being submitted. 但是在这种情况下,你可以将this作为参数传递给validationloading ,它将引用正在提交的表单。 I'm assuming this is similar to why you have document.livefeed.message - use getElementById for these. 我假设这类似于为什么你有document.livefeed.message - 使用getElementById这些。
  • You never actually do anything with the return value of the function. 你实际上从不对函数的返回值做任何事情。 You need to return it up the stack: onSubmit="return validationloading();" 你需要将它return堆栈: onSubmit="return validationloading();"

The number one issue is the document.write thing. 第一个问题是document.write事情。 Remove that and the .submit() after it, and everything will be fine. 删除它和之后的.submit() ,一切都会好的。 There's no need to add a "loading" GIF unless you're using AJAX. 除非您使用AJAX,否则无需添加“加载”GIF。

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

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