Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

jquery - Disable submit button with submitHandler

I am trying to stop a submit button to be clicked multiple times and upload the same item multiple times to MySQL.

Because the validation doesn't work, I can not press the button submit button at all. Which means I can't test the code.

I am running related post as well here, to give you some more information.

What I've got so far, using jQuery validation:

$("#UploadForm").validate({

errorLabelContainer: "#messageBox",
        wrapper: "td",
        rules: {
            auction_description: {
                required: true
            },
            auction_int_postage_type: {
                required: true
            },
            listing_type: {
                required: true
            }
        }

   //all your options here

   submitHandler:function(form){
       $('#submit').attr('disabled','disabled');
   }
});

HTML

 <form method="post" name="UploadForm" id="UploadForm"
 action="upload.php" enctype="multipart/form-data" >
 
 
 <input style="text-transform:none;"  type="text" class="button_date" 
 name="auction_bin_price" id="auction_bin_price" value="" size="15" />
 
 <input class="button2" style="border-right:none; font-size:13px;"
 name="List Item" id="submit" type="submit" value="List Item"/>
 
 </form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a fiddle that has a working demo of solution to your problem.

jQuery 1.6+

To change the disabled property you should use the .prop() function.

$("#submit").prop('disabled', true);
$("#submit").prop('disabled', false);

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

Set the disabled attribute.

$("#submit").attr('disabled','disabled');

To enable again

$("#submit").removeAttr('disabled');

In any version of jQuery

You can always rely on the actual DOM object and is probably a little faster than the other two options if you are only dealing with one element:

// assuming an event handler thus 'this'
this.disabled = true;

The advantage to using the .prop() or .attr() methods is that you can set the property for a bunch of selected items.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...