JavaScript通用表单验证函数

简介:

None.gif 表单定义:
None.gif
< form name = " form1 "  action = ""  style = " behavior:url('form.htc') " ></ form >
None.gif客户端表单验证是由表单的onSubmit事件触发,由于表单的onSubmit事件只能由提交按钮触发,
None.gif所以如果要用函数提交表单form1.submit(),则必须同时执行form1.onSubmit()。
None.gif
None.gif类型定义:
None.gif
None.gif一、整型(
int )
None.gif定义:
None.gifvalueType
= " int "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustInput 必输项(
true / false )
None.gifminInput 最小值(数字)
None.gifmaxInput 最大值(数字)
None.gif举例:
None.gif
< input type = " text "  name = " test "  valueType = " int "  objName = " 总载重吨 "  mustInput = " true "  maxInput = " 10000 " >
None.gif
None.gif二、浮点型(
float )
None.gif定义:
None.gifvalueType
= " float "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustInput 必输项(
true / false )
None.gifminInput 最小值(数字)
None.gifmaxInput 最大值(数字)
None.gifdecimalLen小数位数(数字)
None.gif举例:
None.gif
< input type = " text "  name = " test "  valueType = " float "  objName = " 运价 "  mustInput = " true "  maxInput = " 10000.50 "  decimalLen = " 2 " >
None.gif
None.gif三、字符串(string)
None.gif定义:
None.gifvalueType
= " string "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustInput 必输项(
true / false )
None.gifstringLen 字符串长度(数字)
None.gif举例:
None.gif
< input type = " text "  name = " test "  valueType = " string "  objName = " 英文船名 "  mustInput = " true "  stringLen = " 100 " >
None.gif
None.gif四、日期(date)
None.gif定义:
None.gifvalueType
= " date "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustInput 必输项(
true / false )
None.gif举例:
None.gif
< input type = " text "  name = " test "  valueType = " date "  objName = " 开始日期 "  mustInput = " true " >
None.gif备注:
None.gif日期现在只能校验的格式为(yyyy
- mm - dd)
None.gif
None.gif五、邮箱(email)
None.gif定义:
None.gifvalueType
= " email "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustInput 必输项(
true / false )
None.gif举例:
None.gif
< input type = " text "  name = " test "  valueType = " email "  objName = " 邮箱 "  mustInput = " true " >
None.gif
None.gif六、单选(radio)
None.gif定义:
None.gifvalueType
= " radio "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustSelect 必输项(
true / false )
None.gif举例:
None.gif
< input type = " radio "  name = " test "  valueType = " radio "  objName = " 租船方式 "  mustSelect = " true " >
None.gif备注:
None.gif对于同一组单选按钮,只需要定义第一个即可。
None.gif
None.gif七、复选(checkbox)
None.gif定义:
None.gifvalueType
= " checkbox "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifminSelect 最小选择数(数字)
None.gifmaxSelect 最大选择数(数字)
None.gif举例:
None.gif
< input type = " checkbox "  name = " test "  valueType = " checkbox "  objName = " 爱好 "  minSelect = " 2 "  maxSelect = " 5 " >
None.gif备注:
None.gif对于同一组复选按钮,只需要定义第一个即可。
None.gif
None.gif八、下拉列表框(select)
None.gif定义:
None.gifvalueType
= " select "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifmustSelect 必输项(
true / false )
None.gif举例:
None.gif
< select name = " test "  valueType = " select "  objName = " 租船方式 "  mustSelect = " true " >
None.gif
None.gif九、列表框(list)
None.gif定义:
None.gifvalueType
= " list "
None.gif属性:
None.gifobjName 对象名称(字符串)
None.gifminSelect 最小选择数(数字)
None.gifmaxSelect 最大选择数(数字)
None.gif举例:
None.gif
< select name = " test "  valueType = " list "  objName = " 爱好 "  minSelect = " 2 "  maxSelect = " 5 " >
None.gif
None.gif
None.gif
// ///////////////////////////////////////////////////////////////////////////////////////////////////////
None.gif

None.gif
<!--   ---------------------------------------------------------------------
None.gif
//
None.gif//
 File: form.htc
None.gif//
 version: 1.0
None.gif//
 Description:客户端表单验证.
None.gif//
 author: 伍子
None.gif//
None.gif//
-------------------------------------------------------------------- -->
None.gif
< PUBLIC:COMPONENT id = " formCheck "  urn = " wwb:formCheck " >  
None.gif
< PUBLIC:ATTACH EVENT = " onsubmit "  ONEVENT = " checkForm() " />
None.gif
< script language = " JavaScript " >
None.gif
function  checkForm()
ExpandedBlockStart.gif
{
InBlock.gif
var oForm=event.srcElement;
InBlock.gif
var eles = oForm.elements;
InBlock.gif
//遍历所有表元素
InBlock.gif
for(var i=0;i<eles.length;i++)
ExpandedSubBlockStart.gif
{
InBlock.gif
//是否需要验证
InBlock.gif
var sType=eles[i].valueType;
InBlock.gif
if(sType)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(eles[i].mustInput!=null && eles[i].mustInput)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(trim(eles[i].value)=="")
ExpandedSubBlockStart.gif
{
InBlock.gif
if(eles[i].objName!=null)
ExpandedSubBlockStart.gif
{
InBlock.gifalert(eles[i].objName
+"不可以为空");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"该文本框为必输字段");
ExpandedSubBlockEnd.gif}

InBlock.gifeles[i].focus(); 
InBlock.gifevent.returnValue
=false
InBlock.gif
return false
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
 
InBlock.gif
switch(sType)
ExpandedSubBlockStart.gif
{
InBlock.gif
//整数
InBlock.gif
case "int":
InBlock.gif
if(!checkInt(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//小数
InBlock.gif
case "float":
InBlock.gif
if(!checkFloat(eles[i]))
ExpandedSubBlockStart.gif

InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//字符串
InBlock.gif
case "string":
InBlock.gif
if(!checkString(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//日期
InBlock.gif
case "date":
InBlock.gif
if(!checkDate(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//邮件
InBlock.gif
case "email":
InBlock.gif
if(!checkEmail(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//单选按钮
InBlock.gif
case "radio":
InBlock.gif
if(!checkRadio(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//复选按钮
InBlock.gif
case "checkbox":
InBlock.gif
if(!checkBox(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//下拉列表框
InBlock.gif
case "select":
InBlock.gif
if(!checkSelect(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
//列表框
InBlock.gif
case "list":
InBlock.gif
if(!checkList(eles[i]))
ExpandedSubBlockStart.gif
{
InBlock.gifevent.returnValue
=false;
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifevent.returnValue
=true;
InBlock.gif
return true;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif
/***检查是否为整数***/
None.gif
function  checkInt(ele)
ExpandedBlockStart.gif
{
InBlock.gif
if(!isInt(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"请输入有效整数");
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gif
{
InBlock.gif
if(ele.maxInput!=null && !isNaN(ele.maxInput))
InBlock.gif
if(parseInt(ele.maxInput)<parseInt(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"您输入的"+ convertNullToSpace(ele.objName)+"值应该小于"+ele.maxInput); 
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}
 
InBlock.gif
if(ele.minInput!=null && !isNaN(ele.minInput))
InBlock.gif
if(parseInt(ele.minInput)>parseInt(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"您输入的"+ convertNullToSpace(ele.objName)+"值应该大于"+ele.minInput);
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif
/***检查是否为小数***/
None.gif
function  checkFloat(ele)
ExpandedBlockStart.gif
{
InBlock.gif
if(isNaN(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"请输入有效数字");
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gif
{
InBlock.gif
if(ele.decimalLen!=null && !checkDecimal(ele.value,ele.decimalLen))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"您输入的"+convertNullToSpace(ele.objName)+"值小数位最多为"+ele.decimalLen);
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}
 
InBlock.gif
if(ele.maxInput!=null && !isNaN(ele.maxInput))
InBlock.gif
if(parseInt(ele.maxInput)<parseInt(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"您输入的"+ convertNullToSpace(ele.objName)+"值应该小于"+ele.maxInput); 
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}
 
InBlock.gif
if(ele.minInput!=null && !isNaN(ele.minInput))
InBlock.gif
if(parseInt(ele.minInput)>parseInt(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"您输入的"+ convertNullToSpace(ele.objName)+"值应该大于"+ele.minInput);
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif
/***检查是否为字符串***/
None.gif
function  checkString(ele)
ExpandedBlockStart.gif
{
InBlock.gif
if(ele.stringLen!=null && !isNaN(ele.stringLen))
ExpandedSubBlockStart.gif
{
InBlock.gif
var value=new String(ele.value);
InBlock.gif
if(value.length>parseInt(ele.stringLen))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"您输入的"+convertNullToSpace(ele.objName)+"最大长度为"+ele.stringLen);
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***检查是否为日期格式***/
None.gif
function  checkDate(ele)
ExpandedBlockStart.gif
{
InBlock.gif
if(!isDate(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"请输入有效日期(yyyy-mm-dd)");
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif
/***检查是否为电子邮箱***/
None.gif
function  checkEmail(ele)
ExpandedBlockStart.gif
{
InBlock.gif
if(!isEmail(ele.value))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"请输入有效邮箱");
InBlock.gifele.focus();
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***检查单选按钮是否需要选择***/
None.gif
function  checkRadio(ele)
ExpandedBlockStart.gif
{
InBlock.gif
//var rads = document.getElementsByName(ele.name);
InBlock.gif
eval("var rads="+name+"."+ele.name);
InBlock.gif
var selectCount=0;
InBlock.gif
for(var i=0;i<rads.length;i++)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(rads[i].checked)
ExpandedSubBlockStart.gif
{
InBlock.gifselectCount
++;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(ele.mustSelect!=null && ele.mustSelect)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(selectCount==0)
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"请选择"+convertNullToSpace(ele.objName));
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***检查复选按钮是否需要选择***/
None.gif
function  checkBox(ele)
ExpandedBlockStart.gif
{
InBlock.gif
//var rads = document.getElementsByName(ele.name);
InBlock.gif
eval("var chks="+name+"."+ele.name);
InBlock.gif
var selectCount=0;
InBlock.gif
for(var i=0;i<chks.length;i++)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(chks[i].checked)
ExpandedSubBlockStart.gif
{
InBlock.gifselectCount
++;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
if(ele.minSelect!=null && !isNaN(ele.minSelect))
ExpandedSubBlockStart.gif
{
InBlock.gif
if(selectCount<parseInt(ele.minSelect))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(convertNullToSpace(ele.objName)
+"至少选择"+ele.minSelect+"");
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
if(ele.maxSelect!=null && !isNaN(ele.maxSelect))
ExpandedSubBlockStart.gif
{
InBlock.gif
if(selectCount>parseInt(ele.maxSelect))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(convertNullToSpace(ele.objName)
+"至多选择"+ele.maxSelect+"");
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***检查下拉列表框是否需要选择***/
None.gif
function  checkSelect(ele)
ExpandedBlockStart.gif
{
InBlock.gif
//var rads = document.getElementsByName(ele.name);
InBlock.gif
if(ele.mustSelect!=null && ele.mustSelect)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(ele.selectedIndex==0)
ExpandedSubBlockStart.gif
{
InBlock.gifalert(
"请选择"+convertNullToSpace(ele.objName));
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***检查列表框的选择项数***/
None.gif
function  checkList(ele)
ExpandedBlockStart.gif
{
InBlock.gif
//var rads = document.getElementsByName(ele.name);
InBlock.gif
var selectCount=0;
InBlock.gif
for(var i=0;i<ele.options.length;i++)
ExpandedSubBlockStart.gif
{
InBlock.gif
if(ele.options[i].selected)
ExpandedSubBlockStart.gif
{
InBlock.gifselectCount
++;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifalert(selectCount);
InBlock.gif
if(ele.minSelect!=null && !isNaN(ele.minSelect))
ExpandedSubBlockStart.gif
{
InBlock.gif
if(selectCount<parseInt(ele.minSelect))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(convertNullToSpace(ele.objName)
+"至少选择"+ele.minSelect+"");
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
if(ele.maxSelect!=null && !isNaN(ele.maxSelect))
ExpandedSubBlockStart.gif
{
InBlock.gif
if(selectCount>parseInt(ele.maxSelect))
ExpandedSubBlockStart.gif
{
InBlock.gifalert(convertNullToSpace(ele.objName)
+"至多选择"+ele.maxSelect+"");
InBlock.gifele.focus(); 
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***判断是否为整数***/
None.gif
function  isInt(s)
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif
var patrn=/^[-,+]{0,1}[0-9]{0,}$/;
InBlock.gif
if (!patrn.exec(s))
InBlock.gif
return false;
InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***判断是否为数字***/
None.gif
function  isNumber(s)
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif
var patrn=/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;
InBlock.gif
if (!patrn.exec(s))
InBlock.gif
return false;
InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***判断是否为日期***/
None.gif
function  isDate(str)
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
InBlock.gif
if(r==null)
ExpandedSubBlockStart.gif
{
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}
 
InBlock.gif
var d= new Date(r[1], r[3]-1, r[4]); 
InBlock.gif
if(!(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]))
ExpandedSubBlockStart.gif

InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***判断是否为邮箱***/
None.gif
function  isEmail(str)
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif
if(str.match(/[\w-]+@{1}[\w-]+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig)!=str)
InBlock.gif
return false;
InBlock.gif
else
InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***将NULL转化为空格,用于显示对象名称***/
None.gif
function  convertNullToSpace(paramValue)
ExpandedBlockStart.gif
{
InBlock.gif
if(paramValue==null)
InBlock.gif
return "";
InBlock.gif
else 
InBlock.gif
return paramValue;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***检查小数位数***/
None.gif
function  checkDecimal(num,decimalLen)
ExpandedBlockStart.gif
{
InBlock.gif
var len = decimalLen*1+1;
InBlock.gif
if(num.indexOf('.')>0)
ExpandedSubBlockStart.gif
{
InBlock.gifnum
=num.substr(num.indexOf('.')+1,num.length-1); 
InBlock.gif
if ((num.length)<len)
ExpandedSubBlockStart.gif
{
InBlock.gif
return true;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gif
{
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return true;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gif
/***去除空格***/
None.gif
function  trim(str)
ExpandedBlockStart.gif
{
InBlock.gif
if (str.length > 0
ExpandedSubBlockStart.gif
{
InBlock.gif
while ((str.substring(0,1== " "&& (str.length > 0)) 
ExpandedSubBlockStart.gif
{
InBlock.gifstr 
= str.substring(1,str.length);
ExpandedSubBlockEnd.gif}

InBlock.gif
while (str.substring(str.length-1,str.length) == " "
ExpandedSubBlockStart.gif
{
InBlock.gifstr 
= str.substring(0,str.length-1);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
return str;
ExpandedBlockEnd.gif}

None.gif
</ script >
None.gif
</ PUBLIC:COMPONENT >  
None.gif



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/07/270754.html,如需转载请自行联系原作者
相关文章
|
19天前
|
JavaScript
js函数封装 —— 金额添加千分位分隔符
js函数封装 —— 金额添加千分位分隔符
16 2
|
6天前
|
存储 资源调度 前端开发
JavaScript 使用axios库发送 post请求给后端, 给定base64格式的字符串数据和一些其他参数, 使用表单方式提交, 并使用onUploadProgress显示进度
使用 Axios 发送包含 Base64 数据和其他参数的 POST 请求时,可以通过 `onUploadProgress` 监听上传进度。由于整个请求体被视为一个单元,所以进度可能不够精确,但可以模拟进度反馈。前端示例代码展示如何创建一个包含 Base64 图片数据和额外参数的 `FormData` 对象,并在上传时更新进度条。后端使用如 Express 和 Multer 可处理 Base64 数据。注意,实际进度可能不如文件上传精确,显示简单加载状态可能更合适。
|
16天前
|
JavaScript
js 延时执行代码的最佳实践 —— 自定义 sleep 函数
js 延时执行代码的最佳实践 —— 自定义 sleep 函数
13 0
|
17天前
|
JavaScript
js 高频实用函数封装汇总(持续更新)
js 高频实用函数封装汇总(持续更新)
13 0
|
18天前
|
JavaScript
js 数组移除指定元素【函数封装】(含对象数组移除指定元素)
js 数组移除指定元素【函数封装】(含对象数组移除指定元素)
13 0
|
18天前
|
JavaScript
js 调试 —— 断点(含进入函数、条件断点等)
js 调试 —— 断点(含进入函数、条件断点等)
19 0
|
18天前
|
JavaScript
JS【详解】函数.bind()
JS【详解】函数.bind()
7 0
|
18天前
|
JavaScript
js 【详解】函数中的 this 指向
js 【详解】函数中的 this 指向
14 0
|
18天前
|
移动开发 前端开发 JavaScript
|
18天前
|
存储 JavaScript 前端开发