最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable submit button on first click with Jquery - Stack Overflow

programmeradmin1浏览0评论

I currently have a lightbox popup attached to a submit button that shows only the first time the submit button is clicked. Basically before someone submits a form, we want them to see this popup when they hit the submit button. And that all works fine, but now I need to make it to where on that first click, the form doesn't submit/process. However, after that first click, the submit button would need to be enabled to where they can submit the form.

Any idea how to change the below code to where the submit button does not process the form only the first time the submit button is clicked?

   <script type="text/javascript">
    $('#Submitbutton').one("click",function(e) {

    $('#lp').lightbox_me({
    centered: true,
    overlayCSS:{background: '#481d33', opacity: .45},
    overlaySpeed:0,
    lightboxSpeed:0
    });

    });
    </script>

I currently have a lightbox popup attached to a submit button that shows only the first time the submit button is clicked. Basically before someone submits a form, we want them to see this popup when they hit the submit button. And that all works fine, but now I need to make it to where on that first click, the form doesn't submit/process. However, after that first click, the submit button would need to be enabled to where they can submit the form.

Any idea how to change the below code to where the submit button does not process the form only the first time the submit button is clicked?

   <script type="text/javascript">
    $('#Submitbutton').one("click",function(e) {

    $('#lp').lightbox_me({
    centered: true,
    overlayCSS:{background: '#481d33', opacity: .45},
    overlaySpeed:0,
    lightboxSpeed:0
    });

    });
    </script>
Share Improve this question asked Jul 9, 2014 at 6:38 user1610904user1610904 3974 silver badges21 bronze badges 1
  • can you show more sample code? or express details. – cyberoot Commented Jul 9, 2014 at 6:46
Add a ment  | 

6 Answers 6

Reset to default 5
var hasClicked = false;
$('#Submitbutton').on('click',function(e) {
    if (hasClicked === true) {
        //submit form here
    } else {
        e.preventDefault();
        hasClicked = true;
        $('#lp').lightbox_me({
            centered: true,
            overlayCSS:{background: '#481d33', opacity: .45},
            overlaySpeed:0,
            lightboxSpeed:0
        });
    }
});

Sets a variable on first submit, then on second submit does something different because of that variable.

Edit: Consistent quotes and code cleanup.

Change .on() from .one(). You can declare a variable to track whether button was clicked or not.

var isAlreadytClicked = false;
$('#Submitbutton').on("click", function (e) {
    if (isAlreadytClicked == false) {
        isAlreadytClicked = true;
        return;
    }

    if (isAlreadytClicked) {
        //Do whatever you want on subsequent click

    }
});

add the line

$('#SubmitBtn').attr('disabled',false);

inside the if block.

You can use the counter to determine clicked count:

 var count = 0;
 $('#Submitbutton').click(function(e) {
 count++;
 if(count==1)
 return;//return if clicked first time
 $('#lp').lightbox_me({centered: true,overlayCSS:{background: '#481d33', opacity: .45},overlaySpeed:0,lightboxSpeed:0});
 });
<input type="button"  id="SubmitBtn" value="Submit" onclick="return submitData();" disabled="disabled" >

In Script

//create a global variable for clicked or not
var submitBtnClicked = false;

function submitData()
{
 if(submitBtnClicked )
 {
   $('#SubmitBtn').attr('disabled',false);
   submitBtnClicked =true;
 }    
}

simply a return false should do. alternatively e.perventDefault() may help for event bubbling.

  $('#Submitbutton').one("click",function(e) {

     $('#lp').lightbox_me({
     centered: true,
     overlayCSS:{background: '#481d33', opacity: .45},
     overlaySpeed:0,
     lightboxSpeed:0
     });
    e.perventDefault();
     return false;
  });
发布评论

评论列表(0)

  1. 暂无评论