input file检验成功之后才可以点击

input file检验成功之后才可以点击

需求

在上传发票前需要先填写发票号,然后点击选择文件直接完成上传功能

实现思路

在没有输入发票号之前,file按钮不可用不能点击,输入之后,按钮可用,点击之后选择文件,选择文件之后直接完成上传

html代码

有一个文本框

html 复制代码
<div id="uploadFrom" class="easyui-dialog" title="上传发票" data-options="iconCls:'icon-save',closed: true,modal: true," style="width:400px;height:200px;padding:10px">
            <div style="margin-bottom: 20px;">
                发票号码:<input type="text" id="spuId" name="spuId" class="easyui-textbox" data-options="require:true"/>
            </div>
            <div>
                <input type="file" id="fileInput" accept="image/*,.pdf" onchange="uploadFile()" disabled/> <!-- 隐藏文件输入框 -->
            </div>
        </div>

js代码

javascript 复制代码
/**
 * 页面加载立即执行查询
 */
$(document).ready(function () {
    check();
});

/*输入检查*/
function check(){
    let input = $('#spuId');
    let fileInput = $('#fileInput');
    input.textbox('textbox').bind('keyup', function(e){
        let val = input.textbox('textbox').val()
        if(val === ''){
            fileInput.attr("disabled","disabled")
        }else{
            fileInput.removeAttr("disabled")
        }
    });
}

/*上传文件*/
function uploadFile() {
    let fileInput = document.getElementById('fileInput');
    let file = fileInput.files[0]; // 获取文件
    let spuId = $("#spuId").val();
    let businessId = $("#businessId").val();
    $.messager.progress();
    if (file) {
        let formData = new FormData();
        formData.append('file', file); // 将文件添加到 FormData 对象中
        formData.append('spuId',spuId)
        formData.append('businessId',businessId)
        formData.append('businessType','16')
        // 发送 AJAX 请求上传文件
        $.ajax({
            url: '${ctxBase}/common/file/upload',
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(response) {
                initFileTable()
                $.messager.alert('提示', '上传成功!', 'success');
                updateInvoiceNo(businessId);
                $.messager.progress('close');

            },
            error: function(jqXHR, textStatus, errorThrown) {
                $.messager.alert('提示', '上传失败!', 'error');
                $.messager.progress('close');
            }
        });
    } else {
        $.messager.alert('提示', '请上传文件!', 'info');
        $.messager.progress('close');
    }
}

这里使用的是easyui-textbox,如果使用input 可以使用以下方法监听

javascript 复制代码
function check(){
    let input = $('#spuId');
    let fileInput = $('#fileInput');
    input .addEventListener('keyup', function() {
      let val = input.val()
        if(val === ''){
            fileInput.attr("disabled","disabled")
        }else{
            fileInput.removeAttr("disabled")
        }
    });
}

这样就只有我们输入正确的发票之后,选择文件才能点击

如果大家有更好的方法欢迎留言讨论

相关推荐
大橙子额5 分钟前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
WooaiJava1 小时前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
Never_Satisfied2 小时前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌412 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
WeiXiao_Hyy2 小时前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
xjt_09013 小时前
基于 Vue 3 构建企业级 Web Components 组件库
前端·javascript·vue.js
我是伪码农3 小时前
Vue 2.3
前端·javascript·vue.js
辰风沐阳4 小时前
JavaScript 的宏任务和微任务
javascript
夏幻灵5 小时前
HTML5里最常用的十大标签
前端·html·html5
冰暮流星5 小时前
javascript之二重循环练习
开发语言·javascript·数据库