動態建立表單及建立表單內元素

前段時間項目碰到一個這樣的問題,利用URL來傳遞參數,若是參數裏面包含了‘批’字,參數到後臺後已,「批」字變爲亂碼。影響了數據的查詢。一直解決不了這個問題,後來改用POST提交方式,把此問題規避了。如今把這段動態建立隱藏Form,而後自動提交的代碼放在這裏記錄下:

//這個方法是自動建立一個Form表單,並指定表單的提交方式是postapp

window.getNewSubmitForm = function()
{
      var submitForm = document.createElement("FORM");
      document.body.appendChild(submitForm);
      submitForm.method = "POST"; 
      return submitForm;
}post

//朝方法1建立的表單裏面動態的建立元素
window.createNewFormElement = function(inputForm, elementName, elementValue)
{
      var newElement = document.createElement("<input name='"+elementName+"' type='hidden'>");
      inputForm.appendChild(newElement);
      newElement.value = elementValue;
      return newElement;
}orm

//準備好了,進行提交
window.openPostRequest= function(actionUrl,paras)
{    
      var submitForm = getNewSubmitForm();
      for (i in paras) {
         var _propertyName = i.toString();
         var _propertyValue = paras[_propertyName];
         createNewFormElement(submitForm, _propertyName, _propertyValue);
         }
      submitForm.action= actionUrl;
      submitForm.submit();
}對象

 

調用方法以下:ci

1: 首先建立一個Object對象,把要建立的元素放到這個對象內element

 var object = new Object();
            object.queryName = null;
            object.updateFlag = null;     
            object.contentType=contentType;
            object.keyWords=keyWords;
            object.curStatus=curStatus;
            object.spId=spId;
            object.name=name;          
            object.cid=cid;
            object.subPloyId=subPloyId;
            object.singerName=singerName;
            object.province=province;
            object.pageSize=pageSize; 
            object.from=null;
            object.to=null;      get

2:將元素傳入input

  openPostRequest('/mdmc/spOutputOfficeData.cmd',object);  cmd

相關文章
相關標籤/搜索