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

I'm trying to disable a submit button when a file is not selected following a working solution onl

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648784a4638133.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信