About eval ()

Source: Internet
Author: User

When you write an HTML program, you need to convert string-type data to int-type data and then perform four hybrid operations. For example:
VaR A = "12"; var B = "23 ";
Alert (A + B );
The output in the above example is "1223". This can only be a string connection. If eval () is used, will it always report an error like Nan?
I think yes, because this is not the case, because you have not figured out how to use the eval () function.

My program has an example like this: We get data from the front-end text box, and then perform four arithmetic operations on the data obtained from the data.

1 nowYear = document.getElementById("TxtStyleTwoY").value;2 nowMonth = document.getElementById("TxtStyleTwoM").value;3 nowDay = document.getElementById("TxtStyleTwoD").value;

Arithmetic Operations:

1 If (nowyear = starty) 2 {3 if (nowmonth> startm) 4 {5 for (M = startm; m <nowmonth; ++ m) // calculate the total number of days in a month. 6 {7 days = days + modays [m]; 8} 9 days + = (nowday-startd); 10 document. getelementbyid ("Answer "). value = days + "days at" + nowyear + "year" + nowmonth + "month" + nowday + "day. ";
11} 12 else if (nowmonth = startm) 13 {14 if (nowday> = startd) 15 {16 days = days + nowday-startd; // calculate the number of days in the last month 17 document. getelementbyid ("Answer "). value = days + "days at" + nowyear + "year" + nowmonth + "month" + nowday + "day. "; 18} 19 else20 document. getelementbyid ("Answer "). value = "date error! "; 21 22}

 

The above is to directly obtain the string, in fact, we only need to make a small conversion to the above Code, the program can run perfectly:

1 nowYear = eval(document.getElementById("TxtStyleTwoY").value);2 nowMonth = eval(document.getElementById("TxtStyleTwoM").value);3 nowDay = eval(document.getElementById("TxtStyleTwoD").value);

I don't know if I have made it clear that the document is not very good. Don't be surprised... *_*

 

 

 

 

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.