简体   繁体   English

jQuery简单修改,以便在单击图像时禁用单击图像

[英]jQuery simple modification to disable click on image when it was clicked once

jQuery simple modification to disable clicking jQuery简单修改禁用点击

The following jQuery make use of an image submit button of a the JqPostForm form to post. 以下jQuery使用JqPostForm表单的图像提交按钮发布。

How to change the image from image1.png to image3.png when clicked and disable further clicking ? 单击时如何将图像从image1.png更改为image3.png并禁用进一步单击? and how to remove the Thank you message after 2 sec ? 以及如何在2秒后删除谢谢消息?

There is no need of keeping the form. 没有必要保留表格。

<script>
$(function(){
    $("#JqPostForm").submit(function(e){
       e.preventDefault();   

        $.post("add.php",
        function(data){

                $("#message_post").html("Thank you");

        });
    });

$('#submit').hover(
            function(){ // Change the input image's source when we "roll on"
                $(this).attr({ src : 'image2.png'});
            },
            function(){ // Change the input image's source back to the default on "roll off"
                $(this).attr({ src : 'image1.png'});             }
        );

});
</script>

<form id="JqPostForm">

<input type="image" name="submit" id="submit" src="image1.png">

</form>
<div id="message_post"></div>
  1. Use $(this).unbind('click').attr('disabled', 'disabled'); 使用$(this).unbind('click').attr('disabled', 'disabled'); on the submit button to disable it. 在提交按钮上禁用它。
  2. Use setTimeout(function () { $("#message_post").hide(); }, 2000); 使用setTimeout(function () { $("#message_post").hide(); }, 2000); to hide the message after 2 seconds. 在2秒后隐藏消息。

     <script> $(function(){ $("#JqPostForm").submit(function(e){ e.preventDefault(); $.post("add.php", function(data){ $("#message_post").html("Thank you"); setTimeout(function () { $("#message_post").hide(); }, 2000); }); }); $('#submit').hover( function(){ // Change the input image's source when we "roll on" $(this).attr({ src : 'image2.png'}); }, function(){ // Change the input image's source back to the default on "roll off" $(this).attr({ src : 'image1.png'}).unbind('click').attr('disabled', 'disabled'); } ); }); </script> <form id="JqPostForm"> <input type="image" name="submit" id="submit" src="image1.png"> </form> <div id="message_post"></div> 

I'm assuming you still want it to act like a submit button. 我假设你仍然希望它像提交按钮一样。 I would bind a click handler, then change the image and disable the input and unbind the handler: 我会绑定一个单击处理程序,然后更改图像并禁用输入并取消绑定处理程序:

$('#submit').click(function(e){ 
    $(e.target).unbind('click').attr('src','image3.png').attr('disabled', true);
});

I missed the part about removing the thank you message... You just need to add a timeout to your $.post callback, like: 我错过了关于删除谢谢消息的部分...你只需要在$ .post回调中添加超时,例如:

$.post("add.php",
    function(data){
        $("#message_post").html("Thank you");
        setTimeout("$('#mesage_post').html('');", 5000);
    });

i took just the part of submitting form: 我只是提交表格的一部分:

$("#JqPostForm").submit(function(e){
   e.preventDefault();   

    $.post("add.php",
    function(data){
            $("#submit").attr('src','image3.png');
            $("#submit").attr('disabled',true);
            $("#submit").unbind('click');
            $("#message_post").html("Thank you");

    });
});

You can always use the jquery .one() 你总是可以使用jquery .one()

http://api.jquery.com/one/ http://api.jquery.com/one/

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

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