Substitute for eval() for using LiveValidation constructor
I'm using LiveValidation for input elements. These come in a table retrieved with an AJAX call, and can be multiples of 4, anywhere between 4 and 36.
I'm using eval to call the constructor for LiveValidation as the number of input elements can vary after each AJAX call and I couldn't think of another way (I don't have much experience with JavaScript).
I'm using this:
$("input[type=text]", tableElement).each(function(index) {
eval("var temp_" + index + " = new LiveValidation(this, { wait: 0, validMessage: ' ' });");
eval("temp_" + index + ".add(Validate.Numericality, { onlyInteger: true });");
eval("temp_" + index + ".add(Validate.Presence, { failureMessage: 'Cannot be bl开发者_JAVA技巧ank' });");
});
What would be a better way of doing achieving the same without the use of eval as I know it should be used very sparingly.
Well, seems like I can just use this:
$("input[type=text]", tableElement).each(function(index) {
var temp = new LiveValidation(this, { wait: 0, validMessage: ' ' });
temp.add(Validate.Numericality, { onlyInteger: true });
temp.add(Validate.Presence, { failureMessage: 'Cannot be blank' });
});
I thought that that wouldn't work, but it does.
精彩评论