本文實例講述了js實現接收表單的值並將值拼在表單action後面的方法。分享給大家供大家參考,具體如下:
今天遇到一個問題,在form表單中有若干個input,其中有一個上傳文件的input,現在需要在點擊提交按鈕時,將不是文件類型的input的值得到並拼成&name=value的格式加在action後面,這樣就能一同傳出去了
<form id="myform" name="myform" method="post" action="http://www.yoursiteweb.com:8080/justsy/Dolet?type=ws_justsy_webclips_policy_update" 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)) ? /[^.]+$/.exec(filename.toLowerCase()) : ''; if(fileExt!='jpg'){ document.getElementById("IconFile_error").innerHTML=""; alert("請上傳圖片"); return false; } document.getElementById("myform").action+=getParams(); document.getElementById("myform").submit(); } }
這樣就能拼過去了....挺好用的....還能檢驗checkbox中的值...選中為1,不選中為0
希望本文所述對大家JavaScript程序設計有所幫助。