Write two common functions. use JavaScript to obtain form data.

Source: Internet
Author: User

During this time, JavaScript is often used to obtain the submitted data in various forms. Considering code consistency and logic simplification, the following two JS functions are created to obtain data in form in a unified manner. Data is obtained based on formid and inputid.

The function call method is as follows.

Obtain data:

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, 'logicter ');

Set Data
If (charstring) setvalue (formid, 'charstring', charstring );

The Form format is as follows:

Test condition <textarea name = "testcondition" class = "inputsingle" id = "testcondition"> </textarea>
<! -- <Textarea name = "xmldata" class = "inputsingle" id = "xmldata"> </textarea> -->
<Br>

Low-value data <input class = "inputlow" name = "lvalue" id = "lvalue" require = "false" datatype = "double" MSG = "low-value data must be numerical data "> <br>
Operator symbol <select name = "logicterm" id = "logicterm">
<Option value = "~ "> Between </option>
<Option value = "="> equal to </option>
<Option value = ">="> greater than or equal to </option>
<Option value = "<="> less than or equal to </option>
<Option value = ">"> greater than </option>
<Option value = "<"> less than </option>
<Option value = ""> none </option>
</SELECT> <br>

As shown above, all input values of different types are consistent with the set values. Note that each input must have an id attribute, and form must also

Two JS functions are supported:

// Ignore the type and set the value of input data in Form
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 = 'den den '){
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 ')
}
}
}
}
}
}
// Ignore the type and set the value of the input data in Form
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 = 'den den '){
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 '){
// Calculate the number of options
// Cyclically checks the number and
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;
}
}
}
// Check data cyclically
}
}
}
Return tempvalue;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.