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")
        }
    });
}

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

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

相关推荐
轻语呢喃7 分钟前
DeepSeek 接口调用:从 HTTP 请求到智能交互
javascript·deepseek
风之舞_yjf38 分钟前
Vue基础(14)_列表过滤、列表排序
前端·javascript·vue.js
belldeep1 小时前
QuickJS 如何发送一封邮件 ?
javascript·curl·smtp·quickjs
Jiaberrr2 小时前
uniapp Vue2 获取电量的独家方法:绕过官方插件限制
前端·javascript·uni-app·plus·电量
Joker`s smile2 小时前
使用React+ant Table 实现 表格无限循环滚动播放
前端·javascript·react.js
然我2 小时前
从原生 JS 到 React:手把手带你开启 React 业务开发之旅
javascript·react.js·前端框架
wkj0012 小时前
QuaggaJS 配置参数详解
java·linux·服务器·javascript·quaggajs
代码搬运媛2 小时前
React 中 HTML 插入的全场景实践与安全指南
安全·react.js·html
阿琳a_3 小时前
前端对WebSocket进行封装,并建立心跳监测
前端·javascript·vue.js·websocket
Am1nnn3 小时前
【Pinia】Pinia和Vuex对比
前端·javascript·vue.js