使用JavaScript更改Gravity表单字段的值

3
我需要检测Gravity表单字段(第5个表单,第215个字段;是一个数字字段)的更改,将数字四舍五入以便在没有尾随零的情况下获得一个,并将新的、四舍五入的值返回到该字段。我试图拼凑一些其他代码片段,但我一定做错了什么。我在JavaScript方面是一个绝对的菜鸟。
我使用Gravity Wiz的“Gravity Forms Custom JavaScript”插件,在包含适当表单的页面上插入此脚本。
jQuery(“#gform_fields_215”).on(“change”,”#input_215″, ( function(e) {
var number = document.getElementById("input_215");
var rounded = number.toFixed(2);
document.getElementById("input_215").value = rounded;
}

我做错了什么?可能是一切都错了!哈哈

1个回答

0

我知道你已经忘记你曾经问过这个问题了,但这是解决方案。

jQuery('#gform_fields_215').on('change','#input_215', ( function(e) {
  var number = document.getElementById("input_3_1_5").value;
  var rounded = parseInt(number).toFixed(2);
  document.getElementById("input_215").value = rounded;
}

你已经使用了jQuery和vanilla js语法。如果你只使用jQuery,可以按照以下方式完成。

jQuery("#gform_3").on("change", "#input_3_1_5", function (e) {
  $(this).val(parseInt(this.value).toFixed(2));
});
  • 元素值是字符串,需要在添加小数位之前将其解析为数字
  • this.value 可用于获取当前元素的值

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接