快活林资源网 Design By www.csstdc.com
本文实例讲述了js实现接收表单的值并将值拼在表单action后面的方法。分享给大家供大家参考,具体如下:
今天遇到一个问题,在form表单中有若干个input,其中有一个上传文件的input,现在需要在点击提交按钮时,将不是文件类型的input的值得到并拼成&name=value的格式加在action后面,这样就能一同传出去了
<form id="myform" name="myform" method="post" action="http://www.yoursiteweb.com:8080/justsy/Dolet" enctype="multipart/form-data"> <!-- 后台生成json --> <script type="text/javascript"> var data = [{label:"基本信息",type:"title",tag:"h1"}, {label:"Name",type:"input",input:"text",optional:false,name:"policyName",id:"policyName",value:""}, {label:"Description",type:"input",input:"text",optional:false,name:"policyDescription",id:"policyDescription",value:""}, {label:"",type:"title",tag:"h1"}, {label:"Label(Required)",type:"input", input:"text", optional:true, name:"Label",id:"Label",value:""}, {label:"URL(Required)",type:"input", input:"text", optional:true, name:"URL",id:"URL",value:""}, {label:"Removeable",type:"input", input:"checkbox", optional:false,name:"IsRemovable",id:"IsRemovable",value:"",accept:"image/* "}, {label:"Icon",type:"input", input:"file", optional:true, name:"picurl",id:"IconFile",value:""}, {label:"Precomposed Icon",type:"input", input:"checkbox",optional:false,name:"Precomposed",id:"Precomposed",value:""}, {label:"Full screen",type:"input", input:"checkbox",optional:false,name:"FullScreen",id:"FullScreen",value:""}, {type:"hidden",name:"policyID",id:"policyID",value:""}, {type:"hidden",name:"PayloadDescription",id:"PayloadDescription",value:""}, {type:"hidden",name:"PayloadDisplayName",id:"PayloadDisplayName",value:""}, {type:"hidden",name:"PayloadIdentifier",id:"PayloadIdentifier",value:""}, {type:"hidden",name:"PayloadOrganization",id:"PayloadOrganization",value:""}, {type:"hidden",name:"PayloadType",id:"PayloadType",value:""}, {type:"hidden",name:"PayloadUUID",id:"PayloadUUID",value:""}, {type:"hidden",name:"PayloadVersion",id:"PayloadVersion",value:""}, //{type:"hidden",name:"Icon",id:"Icon",value:""}, {type:"submit",optional:true,func:""} ]; inputs(data);//在需要显示输入框的地方调用该方法 </script> <div class="btn-bar"> <input type="button" onClick="submitForm()" value="保存"> </div> </form>
主要js代码:
function getParams() { var docList=document.getElementById("inputList").getElementsByTagName("input"); var str=""; //alert(docList.length) ; for(var i=0;i<docList.length-1;i++) { if(docList[i].getAttribute("type")=="checkbox"){ if(docList[i].checked) str+=getParam(docList[i].getAttribute("name"),"1") else str+=getParam(docList[i].getAttribute("name"),"0") } else str+=getParam(docList[i].getAttribute("name"),docList[i].value); } return str; } function getParam(key,value) { return "&"+key+"="+value; } function submitForm(){ if(document.getElementById("policyID").value == "") { alert("该公司没有创建策略"); return false; } else{ document.getElementById("IconFile_error").innerHTML="正在提交……"; /*获取文件上传的文件名称并且判断扩展名是否为JPG*/ var testmsg=document.getElementById("IconFile").value; var filename=testmsg.replace(/.*(\/|\\)/, ""); var fileExt=(/[.]/.exec(filename)) "IconFile_error").innerHTML=""; alert("请上传图片"); return false; } document.getElementById("myform").action+=getParams(); document.getElementById("myform").submit(); } }
这样就能拼过去了....挺好用的....还能检验checkbox中的值...选中为1,不选中为0
希望本文所述对大家JavaScript程序设计有所帮助。
快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com
暂无评论...
更新日志
2025年01月17日
2025年01月17日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]