简体   繁体   English

jQuery自定义验证规则问题

[英]jQuery custom validation rule question

I have a random math generator and a .get command that sets the value from the returned data. 我有一个随机数学生成器和一个.get命令,用于从返回的数据中设置值。 For instance the input #Captcha would get it's value as such: 例如,输入#Captcha将获得其值,如下所示:

$.get('PHP/random.php',
    function(data) {
    $("#Captcha").val(data);
});

If the returned data was something like 2 + 5 how would you create a rule that turns the value into two integers, strips out the "+" and then combines the two integers? 如果返回的数据类似于2 + 5,那么您将如何创建一个规则,将值转换为两个整数,去除“ +”,然后将这两个整数合并?

Not sure if this is exactly what you're looking for. 不知道这是否正是您要找的东西。 But if you just to add the two (or more) numbers together this should work: 但是,如果您只是将两个(或多个)数字相加,则应该可以:

var test = "2+5";
var numbers = test.split('+');
var result = 0;
for (var i = 0; i < numbers.length; i++){
    result += parseInt(numbers[i]);
}

Working example - http://jsfiddle.net/L8VMH/ 工作示例-http://jsfiddle.net/L8VMH/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM