這段時間,經常用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;
}