简体   繁体   English

如何在提交表单时使用javascript显示隐藏的div?

[英]How to show a hidden div with javascript when submitting a form?

I'm not really familiar with jquery, but am trying to get a simple animated gif to show when a form is submitted. 我不是很熟悉jquery,但是我想尝试一个简单的动画gif来表示提交表单的时间。 When clicking 'submit' to upload an image I want to show gif so that users know that image is being uploaded. 单击“提交”以上传图像时,我想显示gif,以便用户知道正在上传图像。 The submit button has an id="submit" and also an onClick="return confirm('message')" 提交按钮有一个id =“submit”,还有一个onClick =“return confirm('message')”

I have the div code containing the gif: 我有包含gif的div代码:

<div id="loading" style="display:none">

    <img src="images/hand_timer2.gif" alt="loading" />

</div>

which is hidden. 这是隐藏的。 And it does show if I remove the style. 它确实显示我是否删除了样式。 Fair enough. 很公平。 But when I try to show it with the following javascript it doesn't show: 但是,当我尝试使用以下javascript显示它时,它没有显示:

<script type="text/javascript">

    $(function(){
        $('#submit').click(function(){
        $('#loading').show();

        }); 
    });

I have 我有

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"    
type="text/javascript"></script>

in a separate PHP header file. 在一个单独的PHP头文件中。 As far as I can see it's the only reference to jquery library, but I do have other javascript codes that all work. 据我所知,这是对jquery库的唯一引用,但我确实有其他javascript代码都可以工作。 I just can't get this one to work. 我只是不能让这个工作。 Can anyone point out what I'm doing wrong and why I can't get the div to show gif when clicking submit? 任何人都可以指出我做错了什么以及为什么我不能让div在点击提交时显示gif?

I believe the problem could be that your inline onClick="return confirm('message')" prevent the click-event from reaching your click-event listener attached with jQuery - not sure though. 我相信问题可能是你的内联onClick="return confirm('message')"阻止click事件到达你用jQuery附加的click事件监听器 - 但不确定。 Anyhow, instead of listening for a click-event on the submit-button, I would listen for the submit event on the form, that will fire when the form is actually submitted (a form can usually be posted by other means than clicking the submit button as well - through the Enter key for instance). 无论如何,我不会在提交按钮上收听点击事件,而是会在表单上收听提交事件,这会在表单实际提交时触发(表单通常可以通过其他方式发布,而不是单击提交按钮也是 - 例如通过Enter键)。

$('#idOfYourForm').on("submit", function () {
    $('#loading').show();
});

Side note: 边注:

You don't close the style attribute properly on your loading div (notice that the > is blue): 您没有在加载div上正确关闭样式属性(注意>是蓝色):

<div id="loading" style="display:none>

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

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