Problem with disabling submit buttons on form submit

和自甴很熟 提交于 2020-01-02 08:02:33

问题


Greetings

I am using the jquery disable on submit plug-in but I have a problem. If I disable the submit buttons they dont get passed back to the server so I cant tell which button was pressed. Is this normal? Is there anything I can do about it?

I really dont want to retool my website so I have to set a variable on form submission to tell which button was pressed.

Any ideas ?


回答1:


Here's a workaround I just found in a jQuery forum:

<script type="text/javascript">
    $(document).ready(function() {
        $("#sendSearch").click(function() {
            $('#loadingDiv').show();
            $('#sendSearch').attr("disabled", "disabled");

            // these two lines are the workaround
            this.form.submit();
            return true;
        });
    });
</script>



回答2:


How to Disable the Submit Button of a Web Form

This method hides the button instead of disabling it, and programmatically inserts a disabled <button> tag to make it appear just like the submit button was disabled. Works great.




回答3:


You could do the submit via jquery and disable the button afterwards:

<input type="submit" value="do it!" onclick="$('#mainform').submit(); $(this).attr('disabled','disabled' ); $('#pleasewait').show();" />

EDIT: I forgot form.submit() is not asynchronous. You can do an ajax request instead:

$.ajax({
url: "someurl",
type:"POST",
cache: false,
dataType: "json",
success:gotIt,
async:true,
timeout:240000,
error:ajaxError,
data:$("#mainform").serialize()
}); 

or you could just .hide() the button, or after clicking it setting a non-functional onClick() handler and styling it to look disabled.




回答4:


Be more simple :)

var formid="#id-form-if-exists"; //Put here the id if exists
$("form"+formid).submit(function(){$("form"+formid+" input").attr("disabled",false);});

YEAH



来源:https://stackoverflow.com/questions/661752/problem-with-disabling-submit-buttons-on-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!