寫兩個通用函數統一javascript擷取form資料的方式

來源:互聯網
上載者:User

這段時間,經常用javascript擷取各種form中的提交資料。考慮到代碼的一致和邏輯的簡化,建立如下兩個js函數用統一的方式擷取form中的資料.根據Formid 和 Inputid 獲得資料

函數調用方式如下德例子.

獲得資料:

var  CharString=getValue(formid,'CharString');
var TestCondition=getValue(formid,'TestCondition');
var Hvalue=getValue(formid,'Hvalue');
var Lvalue=getValue(formid,'Lvalue');
var HunitID=getValue(formid,'HunitID');
var LogicTerm=getValue(formid,'LogicTerm');

設定資料
 if(CharString)  setValue(formid,'CharString',CharString);

Form的形式如下:

測試條件<textarea name="TestCondition" class="inputSingle" id="TestCondition" ></textarea>
<!--<textarea name="XMLDATA" class="inputSingle" id="XMLDATA" ></textarea>-->
<br>

低值資料<input class="inputLow" name="Lvalue" id="Lvalue"  require="false" dataType="Double" Msg="低值資料必須為數實值型別的資料"><br>
操作符號<select name="LogicTerm" id="LogicTerm">
<option value="~">之間</option>
<option value="=">等於</option>
<option value=">=">大於等於</option>
<option value="<=">小於等於</option>
<option value=">">大於</option>
<option value="<">小於</option>
<option value="">無</option>
</select><br>

如上,所有不同類型的input的取值和設值都是一致的。注意,每個Input都必須有id屬性,form也是

兩個支援js函數如下:

//忽略類型,對Form中的Input資料設定值
function setValue(formid,inputid,strvalue) {
 var l = new Number();
 if (!strvalue) return ;
 if (strvalue=='') return ;
 eval('l = document.' + formid + '.elements.length')
 for (var i = 0; i < l; i++) {
  var tempid = new String();
  var temptype = new String();
  eval('tempid= document.' + formid + '.elements[i].name;');
  eval('temptype= document.' + formid + '.elements[i].type;');
  if (tempid==inputid){
   if (temptype=='text'){
    eval('document.' + formid + '.elements[i].value=strvalue;');
   }else if (temptype=='textarea'){
    eval('document.' + formid + '.elements[i].value=strvalue;');
      }else if (temptype=='hidden'){
    eval('document.' + formid + '.elements[i].value=strvalue;');
   }else if(temptype=='select-one'){
    var optionLen=new Number();
    eval('optionLen=(document.' + formid + '.elements[i].options.length)');
    for(var j=0;j<optionLen;j++){
     eval('if (document.' + formid + '.elements[i].options['+j+'].value==strvalue) document.' + formid + '.elements[i].selectedIndex='+j+';');
    }
   }else if(temptype=='radio'){
     eval('if (document.' + formid + '.elements[i].value==strvalue) document.' + formid + '.elements[i].checked=true;');
   }else if(temptype=='checkbox'){
    var strvalues=(','+strvalue+',').split(',');
    for(var j=0;j<strvalues.length;j++){
     if (!(strvalues[j]=='')) {
      eval('if (document.' + formid + '.elements[i].value==strvalues[j] ) document.' + formid + '.elements[i].checked=true; ');
     }
    }   
   }else if (temptype=='select-multiple'){
    var strvalues=(','+strvalue+',').split(',');
    var optionLen=new Number();
    eval('optionLen=(document.' + formid + '.elements[i].options.length)');
    for(var k=0;k<optionLen;k++){
     for(var j=0;j<strvalues.length;j++){
      eval('if (document.' + formid + '.elements[i].options[k].value==strvalues[j]) document.' + formid + '.elements[i].options[k].selected=true')
     }
    }
   }
  }
 }
}
//忽略類型,對Form中的Input資料取值
function getValue(formid,inputid) {
 var tempvalue = new String();
 var l = new Number();
 eval('l = document.' + formid + '.elements.length')
 for (var i = 0; i < l; i++) {
  var tempid = new String();
  var temptype = new String();
  eval('tempid= document.' + formid + '.elements[i].name;');
  eval('temptype= document.' + formid + '.elements[i].type;');
//alert(tempid);
//alert(temptype);
  if (tempid==inputid){
   if (temptype=='text'){
    eval('tempvalue= document.' + formid + '.elements[i].value;');
   }else if (temptype=='textarea'){
    eval('tempvalue= document.' + formid + '.elements[i].value;');
      }else if (temptype=='hidden'){
    eval('tempvalue= document.' + formid + '.elements[i].value;');
   }else if(temptype=='select-one'){
    eval('tempvalue=document.' + formid + '.elements[i].options[document.' + formid + '.elements[i].selectedIndex].value;');
   }else if(temptype=='radio'){
    eval('if(document.' + formid + '.elements[i].checked) tempvalue=document.' + formid + '.elements[i].value;');
   }else if(temptype=='checkbox'){
    var checkValue=new String();
    checkValue="";
    eval('if (document.' + formid + '.elements[i].checked==true) {checkValue=document.' + formid + '.elements[i].value}');
    if(checkValue==''){
    }else{
     if (tempvalue==''){
      tempvalue+=checkValue;
     }else{
      tempvalue+=','+checkValue;
     }
    }  
   }else if (temptype=='select-multiple'){
   //計算Option的個數
   //根據個數迴圈檢測數值,並
    var optionLen=new Number();
    eval('optionLen=(document.' + formid + '.elements[i].options.length)');
    for(var j=0;j<optionLen;j++){
     var checkValue=new String();
     checkValue="";
     eval('if (document.' + formid + '.elements[i].options[j].selected) checkValue=(document.' + formid + '.elements[i].options[j].value);');
     if(checkValue==''){
     }else{
      if (tempvalue==''){
       tempvalue+=checkValue;
      }else{
       tempvalue+=','+checkValue;
      }
     }
    }
    //迴圈檢查資料
   }
  }
 }
 return tempvalue;
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.