简体   繁体   English

将变量值设置为jquery message + jquery.validate

[英]set variable value to jquery message + jquery.validate

How to get value of var total in message, 如何在消息中获取var total值,

and also i tried declare inside function but it gives undefined variable 我也尝试声明里面的函数,但它给出了未定义的变量

var total = '';
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    var fund_old = $("#current_fund").val();
    var fund_new = $("#new_fund").val();

    total = parseFloat(9999999999.999999) - parseFloat(fund_old);

    if (parseFloat(fund_new) <= parseFloat(total)) {
        return true;
    } else {
        return false;
    }
    return true;
}, 'sry sktiman' + total + 'is remaining value');

In result i am getting blank value of total 结果我得到总值的空白值

According to the documentation for jQuery.validator.addMethod() you can use jQuery.validator.format() which generates a function that receives the arguments to the validation method and returns a string message, so creating a function that ignores any arguments and returns a string should work: 根据jQuery.validator.addMethod()的文档,您可以使用jQuery.validator.format()生成一个函数,该函数接收验证方法的参数并返回一个字符串消息,因此创建一个忽略任何参数并返回的函数一个字符串应该工作:

var total = '';
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    var fund_old = $("#current_fund").val();
    var fund_new = $("#new_fund").val();

    total = parseFloat(9999999999.999999) - parseFloat(fund_old);

    if (parseFloat(fund_new) <= parseFloat(total)) {
        return true;
    } else {
        return false;
    }
    return true;
}, function() {return 'sry sktiman' + total + 'is remaining value'});

EDIT 编辑

The fiddle for this solution can be found here (thanks to Sparky for providing the code). 这个解决方案的小提琴可以在这里找到(感谢Sparky提供的代码)。

The optional parameters can be used inside the message. 可以在消息内使用可选参数。

The third argument, the optional parameters, (you call it arg ) can be represented within your code as arg[0] , arg[1] , etc. 第三个参数,可选参数(你称之为arg )可以在你的代码中表示为arg[0]arg[1]等。

Then the corresponding values can be used in your message as {0} , {1} , etc. 然后,相应的值可以在您的消息中用作{0}{1}等。

Do your calculation external to .addMethod() and pass the value in using the arg argument. .addMethod()外部进行计算,并使用arg参数传递值。

$.validator.addMethod("valueNotEquals", function(value, element, arg){

    var fund_old= $("#current_fund").val();
    var fund_new =$("#new_fund").val();

    // do this calculation where the rule is declared. See 'validate()'
    //total =  parseFloat(9999999999.999999) - parseFloat(fund_old);

    if(parseFloat(fund_new) <= parseFloat(total)){
        return true;
    } else {
        return false;
    }
    return true;
},'sry sktiman {0} is remaining value');

Since you didn't show your .validate() or the HTML markup, maybe something like this... 由于您没有显示.validate()或HTML标记,可能是这样的......

$('#myform').validate({
    rules: {
        fund_old: {
            // your rules
        },
        fund_new: {
            valueNotEquals: function() {
                return ($parseFloat(9999999999.999999) - parseFloat($("#current_fund").val()));
            }
        }
    }
});

Crude demo: http://jsfiddle.net/eZm7x/ 原始演示: http//jsfiddle.net/eZm7x/

Try, 尝试,

var total = '';
$.validator.addMethod("valueNotEquals", function(value, element, arg){
  var fund_old= $("#current_fund").val();
  var fund_new =$("#new_fund").val();

 total =  parseFloat(9999999999.999999) - parseFloat(fund_old);

      if(parseFloat(fund_new) <= parseFloat(total)){
        return true;
      }else{
        return false;
      }
 return true;
 },'sry sktiman'+ (parseFloat(9999999999.999999) - parseFloat($("#current_fund").val())) + 'is remaining value');

In addMethod Documentation says: "Add a custom validation method. It must consist of a name (must be a legal javascript identifier), a javascript based function and a default string message." addMethod文档中说:“添加自定义验证方法。它必须包含名称(必须是合法的javascript标识符),基于javascript的函数和默认字符串消息。”

So, you are adding a custom validation function to plugin, and NOT executing that function. 因此,您要为插件添加自定义验证功能 ,而不是执行该功能。

Try to set a field with your custom validation and fill that field to run your custom function. 尝试使用自定义验证设置字段并填写该字段以运行自定义函数。 Just AFTER that check total var value. 只是之后检查total的VAR值。

Just add this line function() {return 'sry sktiman' + total + 'is remaining value'} as your second argument, you can easily use this variable 只需添加此行函数(){return'sry sktiman'+ total +'is remaining value'}作为您的第二个参数,您可以轻松使用此变量

var total = ''; 
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
//Your default code here
}, function() {return 'sry sktiman' + total + 'is remaining value'});

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

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