简体   繁体   English

如何使用Javascript和Prototype毫不费力地禁用提交按钮?

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

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) {
      var value = e.srcElement.defaultValue;
      // This doesn't work, but it needs to
      $(e).insert('<input type="hidden" name="commit" value="' + value + '" />');

      // This causes IE to not submit at all
      $$("input[type='submit']").each(function(v) {v.disabled = true;})
   }

   // This works fine
   Event.observe(window, 'load', function() {
     $$("input[type='submit']").each(function(e) {
        Event.observe(e, 'click', greySubmits);
     });
  });

Anyway, I am pretty close, but I can't seem to get any further. 无论如何,我已经很接近了,但是我似乎再也无法做到。

Thanks for any help at all! 谢谢您的帮助!

Update : Sorry, I guess I wasn't entirely clear. 更新 :对不起,我想我还不太清楚。 I'd like to disable all of the submit buttons when someone clicks a submit button. 当某人单击提交按钮时,我想禁用所有提交按钮。 But I do need to send along the value of the submit button so the server knows which button I clicked, hence the insert call. 但是我确实需要发送Submit按钮的值,以便服务器知道我单击了哪个按钮,因此知道了插入调用。 (Note: insert does not create a child of the element you call it on.) And then after disabling the submit buttons I need to call the containing form of the submit buttons submit call, as IE will not submit after you disable the button. (注意:插入并不会创建调用它的元素的子元素。)然后禁用提交按钮后,我需要调用提交按钮提交调用的含形式,如您禁用按钮后,IE不会提交。 Does that make sense? 那有意义吗?

You need to do exactly what the answer says : 您需要完全按照答案说:

"Do not disable the button in its "onclick", but save it, and do it in form's onsubmit." “不要在按钮的“ onclick”中禁用按钮,而要保存它,并在表单的onsubmit中进行。”

So in greySubmits() keep the line that sets the hidden value, but remove the line that disables all the submit buttons. 因此,在greySubmits()中,保留设置隐藏值的行,但删除禁用所有提交按钮的行。

Then add another event handler in your online - to the form, not the submit buttons - that does the disabling. 然后在您的在线表单中添加另一个事件处理程序-而不是提交按钮-来禁用该事件处理程序。

 function reallyGreySubmits(e) {
     // This causes IE to not submit at all
     $$("input[type='submit']").each(function(v) {v.disabled = true;})
 }

 Event.observe(window, 'load', function() {
     $$("input[type='submit']").each(function(e) {
        Event.observe(e, 'click', greySubmits);
     });
     $$("form").each(function(e) {
        Event.observe(e, 'submit', reallyGreySubmits);
     });
  });

Another option, which I've used is to not disable the submits but to swap visibility between two elements. 我使用的另一个选项是不禁用提交,而是在两个元素之间交换可见性。 On click, mark the submits hidden, and then make visible a div or some other element that displays as "disabled" in their place. 单击时,将提交标记为隐藏,然后使div或其他显示为“已禁用”的元素可见。

document.observe("dom:loaded", function(){
   $$('form').find(function(thisForm) {
      Event.observe(thisForm, 'submit', function(event) {
         $$('input[type="submit"]').find(function(input) {
            input.value = 'Please wait ...';
            input.setAttribute('disabled',true);
            });
         });
      });       
   });

I finally got it to work. 我终于得到它的工作。 Ryan helped so I'll upvote him :-) Here's the code: 瑞安(Ryan)帮助了,所以我会投票给他:-)这是代码:

  function replaceSubmit(e) {
    var el = e.element();
    Element.insert(el, { 'before': '<input type="hidden" name="' + el.name + '" value="' + el.value +'" />'});
  }

  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. fixButtons是这样的,当人们单击后退按钮时,页面将修复所有按钮。 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. 而且,如果您想禁用按钮并且不让它在背面重新启用,则只需给它提供一类禁用。

Here's what I came up with, which is adapted from above. 这是我想出的,是从上面改编的。 Fixed so that it detects a cancelled event propagation using Prototype's stopped attribute. 已修复,使其可以使用原型的stopped属性检测到已取消的事件传播。

Other changes include using longer variable names (I always get confused about whether e is event or element), and a function that removed the replacement inputs if the form submission is cancelled. 其他更改包括使用更长的变量名(我总是对e是事件还是元素感到困惑),以及在取消表单提交后删除替换输入的函数。 In my implementation pressing back on the browser doesn't show the page as it was when the user left it, instead it seems to be refetched (I'm using Rails), so I've removed that part too. 在我的实现中,在浏览器上按回去不会显示页面,就像用户离开页面时一样,而是似乎已被重新提取(我使用的是Rails),因此我也删除了该部分。

I'm using buttons rather than inputs in my application so that part has changed also. 我在应用程序中使用按钮而不是输入,因此该部分也已更改。

function replaceSubmit(event) {
    var element = event.element();
    Element.insert(element, { 'before': '<input type="hidden" name="' + element.name + '" value="' + element.value +'" class="button_replacement">'});
}

function removeReplacementSubmits() {
    $$('input.button_replacement').each(function(button) {
        button.remove();
    });
}

function greySubmits(event) {
    // Don't disable the submit if the submit was stopped with a return(false)
    if (event.stopped === true) {
        removeReplacementSubmits();
    } else {
        $$('button[type="submit"]').each(function(button) {
            button.disabled = true;
            button.innerHTML += '&#133;';
        });
    }
}

Event.observe(window, 'load', function() {
    $$("button[type='submit']").each(function(element) {
        Event.observe(element, 'click', replaceSubmit);
    });
    $$("form").each(function(element) {
        Event.observe(element, 'submit', greySubmits);
    });
});

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

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