评估未在javascript中运行

I am having problems setting the educationflag variable to 1. The problem I am solving is not to call enableEdit.php file if flag is 1.

I want to set the flag to 1 when the control comes in the if condition. Right now the control comes in the if condition but does not set the variable to 1. My code is printed below. I

var educationFlag=0;

function editEducation(class){

   //I send education in class variable. So class ='education';
   var condition=eval(class+'Flag');
   if ( condition == 0 ){
      $.ajax({
        url: "enableEdit.php",
        data: "class="+class,
        success: function(msg) {    
        }
    })
      eval(class+'Flag'=1);
   }
}

Thanks

I don't understand what you're trying to do in your code... You say you want to "not call enableEdit.php if flag is 1". If that's the case, why can't you just do:

if(flag == 1) { ... }

And get rid of your eval statements?

Without trying to fix your underlying issues and stop the pollution of the global scope, this should work. Please note I have renamed the reserved word class to varPrefix

var educationFlag=0;

function editClass(varPrefix) {
   if (window[varPrefix+'Flag'] === 0 ){
      $.ajax({
        url: "enableEdit.php",
        data: "class="+varPrefix,
        success: function(msg) {    
          window[varPrefix+'Flag']=1;
        }
    })
  }
}

I send education in varPrefix variable. So varPrefix ='education';

editClass("education") ...

What is the value of the class variable that is passed into the function? I'll make an educated guess that it's going to be something like education? Is that right?

In that case, the first eval() will effectively be this:

var condition=eval('educationFlag');

and the second will be this:

eval('educationFlag'=1);

The second is failing because the =1 part is not in the string. That's the short answer, which directly solves your problem.

However, the better answer is that eval() is the wrong thing to use. You should never use eval() for this kind of thing. Since educationFlag is a global variable, you can access it via the window object as an array element: this means you can do exactly the same thing, without having to use risky eval() at all.

You can reference it like this: window['educationFlag']

Therefore, your eval() lines can be replaced like this:

var condition=window[class+'Flag'];

and

window[class+'Flag']=1;

Hope that helps.