How can I unobtrusively disable submit buttons with Javascript and Prototype?

后端 未结 4 1648
失恋的感觉
失恋的感觉 2021-01-25 03:19

So I found this recommendation, but I can\'t quite seem to figure out how.

This is the code I originally started with:

   function greySubmits(e) {
              


        
4条回答
  •  无人共我
    2021-01-25 04:02

    I finally got it to work. Ryan helped so I'll upvote him :-) Here's the code:

      function replaceSubmit(e) {
        var el = e.element();
        Element.insert(el, { 'before': ''});
      }
    
      function greySubmits(e) {
        // Don't disable the submit if the submit was stopped with a return(false)
        if (e.returnValue) {
          $$("input[type='submit']").each(function(v) {v.disabled = true;})
        }
      }
    
      function fixButtons() {
        $$("input[type='submit']").each(function(v) {
            if (Element.hasClassName(v, 'disabled')) {
              v.disabled = true;
            } else {
              v.disabled = false;
            }
          });
      }
    
      Event.observe(window, 'load', function() {
          fixButtons();
          $$("input[type='submit']").each(function(e) {
              Event.observe(e, 'click', replaceSubmit);
            });
          $$("form").each(function(e) {
              Event.observe(e, 'submit', greySubmits);
            });
        });
    

    The fixButtons is so that when people click the back button the page will fix all the buttons. And if you want to disable a button and have it not re-enable on a back you just give it a class of disabled.

提交回复
热议问题