开发者

lock all submit button before page fully rendered

开发者 https://www.devze.com 2023-01-06 00:21 出处:网络
may i know how to use jquery to lock all the <input type=\"开发者_C百科submit/button\" ..and only allow submit when page is fully rendered?Because of the use case, I might approach it differently.

may i know how to use jquery to lock all the <input type="开发者_C百科submit/button" .. and only allow submit when page is fully rendered?


Because of the use case, I might approach it differently. Instead of actually disabling the buttons, I would just not allow the submit action to work until the page is loaded. This doesn't require any changes to the existing HTML to work, and your pages won't be rendered useless when JS is disabled:

<script>
   // Keep all submit buttons from working by returning false from onclick handlers
   $('input:submit').live('click', function () {
      return false;
   });

   // After everything loads, remove the the "live" restriction via "die"
   $(window).load(function(){
       $('input:submit').die();
   });
</script>

Update: Forgot to mention to put both this and the script tag to load the jQuery library in your <head> if you want this solution to work. (Thanks for reminding me Mike Sherov).


By default, you have your submit buttons have the disabled attribute set to true:

<input type="submit" disabled="disabled" /> 

Then, once the page loads, you can do:

$('input').removeAttr('disabled');


jQuery

$(document).ready(function(){
  $(":button,:submit").removeAttr("disabled");  
});

HTML

<input type="button" id="button1" value="Button 1" disabled="disabled">
<input type="button" id="button2" value="Button 2" disabled="disabled">
<input type="button" id="button3" value="Button 3" disabled="disabled">
<input type="submit" id="submit" value="Submit" disabled="disabled">


<input id="form-submit" type="submit" disabled="disabled" value="Submit" />

$(document).ready(function() {
    $("#form-submit").removeAttr('disabled');
});


Couldn't you do something like

window.onsubmit=function(){return false;}
window.onload=function(){window.onsubmit='';}

I am not sure how the event bubbling would work with an onsubmit. I'd have to test it. Also, I don't know jquery so I'm not sure how to integrate it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号