如何使用jquery禁用更改按钮

时间:2018-05-10 09:56:50

标签: jquery forms

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

2 个答案:

答案 0 :(得分:0)

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

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

2: - $(this).prop('禁用',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>

&#13;
&#13;
    $(document).ready(function(){
        $("#fileUpload").change(function(){
            //$(this).attr("disabled", true);
            $('#btnTest').prop('disabled', true);
            });
    });
  
&#13;
<!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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以这样做,因为我假设一张图片并检查分辨率,然后我根据要求禁用或启用按钮,并且还清除了选择的颜色
图像,因此用户无法上传错误的格式。

<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>