Today encountered a problem, in the JS comparison two values of the size, according to the normal logic write dead and alive will not work.
The problematic code is as follows:
JS Code
- function Chknum () {
- var appcount = $ ("#appcount"). Val ();
- var intencount = $ ("#intencount"). Val ();
- var unemployment = $ ("#unemployment"). Val ();
- if (appcount = = Null | | appcount = = "") {
- Alert ("The number of candidates cannot be empty!") ");
- return false;
- }
- if (intencount = = Null | | intencount = = "") {
- Alert ("The initial number of recruits cannot be empty!") ");
- return false;
- }
- if (Intencount < Appcount) {
- Alert ("Initial employment should be less than the number of applicants!");
- $ ("#intencount"). Val ("");
- return false;
- }
- if (unemployment > Appcount) {
- Alert ("The number of unemployed should be less than the number of applicants!");
- $ ("#unemployment"). Val ("");
- return false;
- }
- };
This problem tangled for a long time, the internet looked down JS incredibly have an eval function
eval function definition and usage
The eval () function computes a string and executes the JavaScript code in it.
The modified code
JS Code
- function Chknum () {
- var appcount = $ ("#appcount"). Val ();
- var intencount = $ ("#intencount"). Val ();
- var unemployment = $ ("#unemployment"). Val ();
- if (appcount = = Null | | appcount = = "") {
- Alert ("The number of candidates cannot be empty!") ");
- return false;
- }
- if (intencount = = Null | | intencount = = "") {
- Alert ("The initial number of recruits cannot be empty!") ");
- return false;
- }
- if (eval (intencount) > eval (appcount)) {
- Alert ("Initial employment should be less than the number of applicants!");
- $ ("#intencount"). Val ("");
- return false;
- }
- if (eval (unemployment) > eval (appcount)) {
- Alert ("The number of unemployed should be less than the number of applicants!");
- $ ("#unemployment"). Val ("");
- return false;
- }
- };
JavaScript compares two numeric size issues