简体   繁体   English

正确禁用提交按钮

[英]properly disabling the submit button

this is the code that I use to disable the button 这是我用来禁用按钮的代码

            $("#btnSubmit").attr('disabled', 'disabled')
            $("#btnSubmit").disabled = true;

and this is my submit button 这是我的提交按钮

<input id="btnSubmit" class="grayButtonBlueText" type="submit" value="Submit" />

the button although looks disabled, you can still click on it.. This is tested with FF 3.0 and IE6 该按钮虽然看上去已禁用,但仍可以单击它。。这已通过FF 3.0和IE6进行了测试

Am I doing something wrong here? 我在这里做错什么了吗?

If it's a real form, ie not javascript event handled , this should work. 如果它是真实形式,即未处理javascript事件 ,则应该可以。

If you're handling the button with an onClick event, you'll find it probably still triggers. 如果使用onClick事件处理按钮,则可能会触发它。 If you are doing that, you'll do better just to set a variable in your JS like buttonDisabled and check that var when you handle the onClick event. 如果这样做,则最好在JS中设置一个像buttonDisabled这样的变量,并在处理onClick事件时检查var会更好。

Otherwise try 否则尝试

$(yourButton).attr("disabled", "true");

And if after all of that, you're still getting nowhere, you can manually "break" the button using jquery (this is getting serious now): 而且,如果这一切仍然没有完成,您可以使用jquery手动“破坏”按钮(这现在变得越来越严重):

$(submitButton).click(function(ev) {
    ev.stopPropagation();
    ev.preventDefault();
});

That should stop the button acting like a button. 那应该使按钮像按钮一样停止工作。

Depending on how the form submission is handled you might also need to remove any click handlers and/or add one that aborts the submission. 根据表单提交的处理方式,您可能还需要删除所有单击处理程序和/或添加一个中止提交的单击处理程序。

$('#btnSubmit').unbind('click').click( function() { return false; } );

You'd have to add the click handler's again when (if) you re-enable the button. 重新启用按钮时,您必须再次添加点击处理程序。

You need to process Back/Prev button into browser. 您需要将“后退/上一步”按钮处理到浏览器中。 Example bellow 波纹管示例

1) Create form.js: 1)创建form.js:

(function($) {
    $.enhanceFormsBehaviour = function() {
        $('form').enhanceBehaviour();
    }

    $.fn.enhanceBehaviour = function() {
        return this.each(function() {
            var submits = $(this).find(':submit');
            submits.click(function() {
                var hidden = document.createElement('input');
                hidden.type = 'hidden';
                hidden.name = this.name;
                hidden.value = this.value;
                this.parentNode.insertBefore(hidden, this)
            });
            $(this).submit(function() {
                submits.attr("disabled", "disabled");
            });         
            $(window).unload(function() {
                submits.removeAttr("disabled");
            })
         }); 
    }
})(jQuery);

2) Add to your HTML: 2)添加到您的HTML:

<script type="text/javascript">
    $(document).ready(function(){
        $('#contact_frm ).enhanceBehaviour();
    });
</script>

<form id="contact_frm" method="post" action="/contact">
   <input type="submit" value="Send" name="doSend" />
</form>

Done :) 完成:)

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

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