【问题标题】:how to disable button on change using jquery如何使用jquery禁用更改按钮
【发布时间】:2018-10-20 13:35:50
【问题描述】:

我想禁用更改文件上的按钮,并想检查文件是否处于适当的分辨率,如果是,则提交按钮启用,如果不是,则禁用任何建议,请我想使用 jQuery。

【问题讨论】:

  • 请添加一些代码或您的努力,以便我们为您提供帮助,当您什么都不做时我们无法提供帮助尝试工作,如果您失败了,请在此处询问,但至少先尝试一下
  • 感谢您的回复 jagdish 但我只是假设它还没有尝试过但我会尝试确保不会再次发生这种情况请考虑投票
  • 没关系,试试代码并在这里发帖,我一定会帮助你的
  • 是的,可以告诉我们您想做什么。代码总是受欢迎的。每当您尝试遇到错误时,请在此处发布,我们会提供帮助。
  • 没关系,abhishek 放一些代码,我们会帮你投票,所以你可以在这里继续,你是新人,所以请在提问前阅读说明

标签: jquery forms


【解决方案1】:

您可以像假设图像一样执行此操作并检查分辨率,然后我根据要求禁用或启用按钮并清除所选
图片,所以用户不能上传错误的格式。

<script>




    var _URL = window.URL || window.webkitURL;

                                    $("#file").change(function (e) {
                                        var file, img;


                                        if ((file = this.files[0])) {
                                            img = new Image();
                                            img.onload = function () {
                                                //alert(this.width + " " + this.height);
                                                var widthofimage = this.width;
                                                var heightofimage = this.height;

                                                if (widthofimage < 1920 && heightofimage < 850 || widthofimage != 1920 && heightofimage != 850 || widthofimage > 1920 && heightofimage > 850) {
                                                    swal({
                                                        title: "Please review",
                                                        text: "Please Upload Image of 1920 X 850",
                                                        icon: "error",
                                                    });

                                                    document.getElementById("file").value = "";
                                                } else {

                                                    $('input[type="submit"]').removeAttr('disabled');

                                                }

                                            };
                                            img.onerror = function () {
                                                alert("not a valid file: " + file.type);
                                            };
                                            img.src = _URL.createObjectURL(file);


                                        }

                                    });






</script>

【讨论】:

    【解决方案2】:

    您可以通过以下方式禁用html输入元素:

    1 :- $(this).attr("disabled", true);

    2 :- $(this).prop('disabled', true);

    试试下面的代码:

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#fileUpload").change(function(){
            //$(this).attr("disabled", true);
            $('#btnTest').prop('disabled', true);
            });
    });
    </script>
    </head>
    <body>
    <input ID="fileUpload"  runat="server" type="file">
    <button type="button" id="btnTest">CLICK ME</button>
    
    </body>
    </html>
    

        $(document).ready(function(){
            $("#fileUpload").change(function(){
                //$(this).attr("disabled", true);
                $('#btnTest').prop('disabled', true);
                });
        });
      
    <!DOCTYPE html>
        <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      
        </head>
        <body>
        <input ID="fileUpload"  runat="server" type="file">
        <button type="button" id="btnTest">CLICK ME</button>
        
        </body>
        </html>

    【讨论】:

    • @Abhishek Saini 试试这个
    • 感谢回复问题已解决感谢您的帮助@faraz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2010-11-17
    • 2016-06-21
    • 1970-01-01
    相关资源
    最近更新 更多