views:

44

answers:

2

I have a form for uploading images. I'd like to disable submit button, until user selects an image to upload. I'd like to do it with jQuery. Currently I have a JavaScript that prevent user from submitting the form more than once by disabling it on submit. It'd be nice to combine this functionality with the new one.

Here's what I've got now:

<script type="text/javascript">
function submitonce(theform) {
    //if IE 4+ or NS 6+
    if (document.all || document.getElementById) {
        //screen thru every element in the form, and hunt down "submit" and "reset"
        for (i = 0; i < theform.length; i++) {
            var tempobj = theform.elements[i]
            if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
            //disable em
            tempobj.disabled = true
        }
    }
}
</script>
<form name="form" enctype="multipart/form-data" method="post" action="upload.php" onSubmit="submitonce(this)">
 <input type="file" name="my_field" value="" />
 <input type="submit">
</form>
+3  A: 

Try

   $('#my_uploader').change(function() {
      if($(this).val()) {
        $('#my_submit_button').attr('disabled', '');
      } else {
        $('#my_submit_button').attr('disabled', 'disabled');
      }
    });
Aaron Hathaway
The `disabled` attribute is, properly, a Boolean; I'd recommend using `.attr('disabled',true')` / `.attr('disabled',false)`.
David Thomas
Oh, good catch David. Thank you :)
Aaron Hathaway
you should use .removeAttr("disabled")
hunter
That's such an obvious point, @hunter, I'm **ashamed** that it had never occurred to me before you pointed out (sincerely, I'm not (to my shame) being facetious)... =/ +1
David Thomas
In HTML boolean attributes such as disabled and readonly can only legally take the name of the attribute.eg. disabled="disabled" readonly="readonly"Most browsers however accept any value for the attribute as being in the affimative.So the following are equivalent:disabled="disabled"disableddisabled="true"disabled="no"http://www.w3.org/TR/REC-html40/intro/sgmltut.html#didx-boolean_attribute-1
pinkfloydx33
+2  A: 

The following seems to work reliably in Chrome and Firefox (Ubuntu 10.10), I'm unable to check on other platforms at the moment:

jQuery

$(document).ready(
    function(){
        $('input:file').change(
            function(){
                if ($(this).val()) {
                    $('input:submit').attr('disabled',false);
                    // or, as has been pointed out elsewhere:
                    // $('input:submit').removeAttr('disabled'); 
                } 
            }
            );
    });

html

<form action="#" method="post">
    <input type="file" name="fileInput" id="fileInput" />
    <input type="submit" value="submit" disabled />
</form>
<div id="result"></div>

Demo at JS Fiddle.

David Thomas
Thanks, I like the simplicity of this solution. How can it be modified to disable submit button again once the form is submitted?
santa
OK, got it. I've added the following function: $("#uploadForm").submit(function(){ $("input[type=submit]", this).attr("disabled", "disabled"); });
santa
`.attr('disabled',true);` =)
David Thomas