最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Disable submit button when file is not selected - Stack Overflow

matteradmin5PV0评论

I'm trying to disable a submit button when a file is not selected following a working solution online. Is it something to do with the script type?

This is the example I followed: disable submit button until file selected for upload

<html>
        <head>

        <script src="//ajax.googleapis/ajax/libs/jquery/1.10.1/jquery.min.js">
            $(document).ready(
    function(){
        $('input:submit').attr('disabled',true);
        $('input:file').change(
            function(){
                if ($(this).val()){
                    $('input:submit').removeAttr('disabled'); 
                }
                else {
                    $('input:submit').attr('disabled',true);
                }
            });
    });
        </script>
</head>

<body>
   <form action="#" method="post">

            <input type="file" name="fileInput" id="fileInput" />
            <input type="submit" value="submit" disabled />


       </form>
</body>
</html>

I'm trying to disable a submit button when a file is not selected following a working solution online. Is it something to do with the script type?

This is the example I followed: disable submit button until file selected for upload

<html>
        <head>

        <script src="//ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js">
            $(document).ready(
    function(){
        $('input:submit').attr('disabled',true);
        $('input:file').change(
            function(){
                if ($(this).val()){
                    $('input:submit').removeAttr('disabled'); 
                }
                else {
                    $('input:submit').attr('disabled',true);
                }
            });
    });
        </script>
</head>

<body>
   <form action="#" method="post">

            <input type="file" name="fileInput" id="fileInput" />
            <input type="submit" value="submit" disabled />


       </form>
</body>
</html>
Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Jun 4, 2013 at 8:54 EddyEddy 491 gold badge2 silver badges7 bronze badges 3
  • 2 Have you included the jQuery library? – Insyte Commented Jun 4, 2013 at 8:56
  • 2 Your code works fine - jsfiddle/fUvvb. You aren't including the jQuery library here however – dsgriffin Commented Jun 4, 2013 at 8:57
  • Hi, what is actually happening when you select a file? Do you want the button to enable only when the button is pressed or when a file is truely selected? – Jaay Commented Jun 4, 2013 at 8:57
Add a ment  | 

2 Answers 2

Reset to default 3

Try with Jquery declared because the Jquery is not included:

  <html>
<head>
    <script type="text/javascript" src="http://code.jquery./jquery-2.0.0.js"></script>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 


            <script type="text/javascript">
                $(document).ready(
        function(){
            $('input:submit').attr('disabled',true);
            $('input:file').change(
                function(){
                    if ($(this).val()){
                        $('input:submit').removeAttr('disabled'); 
                    }
                    else {
                        $('input:submit').attr('disabled',true);
                    }
                });
        });
            </script>
    </head>

    <body>
       <form action="#" method="post">

                <input type="file" name="fileInput" id="fileInput" />
                <input type="submit" value="submit" disabled />


           </form>
    </body>
    </html>

Include the jQuery library in the <head>

<script src="//ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
Post a comment

comment list (0)

  1. No comments so far