JavaScript 中的 {0} 是什么意思

发布于 2024-09-09 18:34:29 字数 517 浏览 1 评论 0原文

这个答案的代码中:

$.validator.addMethod('lessThanEqual', function(value, element, param) {
    return this.optional(element) || parseInt(value) <= parseInt($(param).val());
}, "The value {0} must be less than {1}");

{ 的含义是什么0}{1}? Javascript 中定义这些参数替换的经验法则是什么?

因此,根据评论,我想进一步询问以便在此处传递{0}和{1}。

我必须使用验证函数 lessThanEqual 的语法是什么。

谢谢

In the code from this answer:

$.validator.addMethod('lessThanEqual', function(value, element, param) {
    return this.optional(element) || parseInt(value) <= parseInt($(param).val());
}, "The value {0} must be less than {1}");

What is the meaning of {0} and {1}? What is the rule of thumb in Javascript to define those parameter replacements?

So based on the comments, I want to further ask in order to pass {0} and {1} here.

What is the syntax I have to use for validation function lessThanEqual.

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殊姿 2024-09-16 18:34:29

没有“特殊”含义,它们只是验证器插件在显示消息时替换的标记,以数字方式引用规则的参数。

具体来说,这种情况发生在插件中:

message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);

因此 {0} 引用第一个参数,{1} 引用第二个参数,依此类推。这是格式函数 jQuery.validate 1.7 中的格式:

$.validator.format = function(source, params) {
  if ( arguments.length == 1 ) 
    return function() {
      var args = $.makeArray(arguments);
      args.unshift(source);
      return $.validator.format.apply( this, args );
    };
  if ( arguments.length > 2 && params.constructor != Array  ) {
    params = $.makeArray(arguments).slice(1);
  }
  if ( params.constructor != Array ) {
    params = [ params ];
  }
  $.each(params, function(i, n) {
    source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
  });
  return source;
};

There isn't a "special" meaning, they're just tokens that the validator plugin replaces when displaying the message, numerically referring to the parameters for the rule.

Specifically, this is happening in the plugin:

message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);

So {0} refers to the first parameter, {1} the second and so on. Here's that format function as it is in jQuery.validate 1.7:

$.validator.format = function(source, params) {
  if ( arguments.length == 1 ) 
    return function() {
      var args = $.makeArray(arguments);
      args.unshift(source);
      return $.validator.format.apply( this, args );
    };
  if ( arguments.length > 2 && params.constructor != Array  ) {
    params = $.makeArray(arguments).slice(1);
  }
  if ( params.constructor != Array ) {
    params = [ params ];
  }
  $.each(params, function(i, n) {
    source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
  });
  return source;
};
可爱咩 2024-09-16 18:34:29

我认为这是验证器插件特定的模板语法,用于将 {0} 绑定到实际值,将 {1} 绑定到“所需”值。

I think it's validator plugin specific templating syntax to bind {0} to the real value and {1} to the "desired" value.

白芷 2024-09-16 18:34:29

它们是占位符。在使用验证器的代码中,{0} 和 {1} 被替换为实际值。

They are placeholders. In the code that uses the validator, {0} and {1} are replaced by actual values.

桃扇骨 2024-09-16 18:34:29

它们没有特殊含义,但用作格式占位符,例如:

function format(str/*, args*/) {
  var args = Array.prototype.slice.call(arguments, 1);
  return str.replace(/{(\d+)}/g, function (m, i) {
    return args[i];
  });
}

format("{0} {1}!!", "Hello", "world"); // "Hello world!!"

They have no special meaning, however they are used as format placeholders, for example:

function format(str/*, args*/) {
  var args = Array.prototype.slice.call(arguments, 1);
  return str.replace(/{(\d+)}/g, function (m, i) {
    return args[i];
  });
}

format("{0} {1}!!", "Hello", "world"); // "Hello world!!"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文