Skip Navigation

[Resolved] Need help with JS math calculation

This thread is resolved. Here is a description of the problem and solution.

Problem:

1. if I set a single line numeric field, that id="original-price", and display the price after 15% discount as id="net-price", How should I do by the shortcode below? just add that id="original-price" in to "[ ]"

2. if I want to restore the value after calculated, and display this on the post, how do I create that custom field and use the value as the value of field?

Solution:

It needs some custom JS codes, see example here:
https://toolset.com/forums/topic/need-help-with-js-math-calculation/#post-907255

Relevant Documentation:

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jeffrey 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#906508

I am trying to return a price after specific discount, and have checked with the part of Js on w3schools.com, in my case, the formula is not difficult, it looks like the z=x*0.85, but in this example below, where I can not set an undefined custom numeric field as a multiplier.
my questions are:

1. if I set a single line numeric field, that id="original-price", and display the price after 15% discount as id="net-price", How should I do by the shortcode below? just add that id="original-price" in to "[ ]"

2. if I want to restore the value after calculated, and display this on the post, how do I create that custom field and use the value as the value of field?

Thank you so much!

[cred_field field='original-price' post='room' value='' urlparam='' class='form-control' id='monthly-rate' output='bootstrap']

<p id="net-price"></p>
<script>
var z = 20* 0.85;
document.getElementById("net-price").innerHTML = z;
</script>
#906574

Hello,

It is a custom codes problem,

Q1) How should I do by the shortcode below? just add that id="original-price" in to "[ ]"
I need detail steps to duplicate the same problem, how do you stup the custom field "original-price"? how and where do you want to add the ID?
You can post the question in Chinese and share the live problem URL.

Q2) Same as above, I need details, where can I see the value? what and how do you want to restore the value?
And I have enabled the private detail box, please provide detail steps to see the problems you mentioned above. thanks

#907255

Q1) [cred_field field='discount' value='' urlparam='' class='form-control' output='bootstrap'] 如果我想指定一个值,而不是允许用户在前端提交的话, 我是不是可以填 value='0.8' 然后我隐藏此字段?
是的,你只需要将默认值“value”设置为0.8,例如:
[cred_field field='discount' value='0.8' urlparam='' class='form-control' output='bootstrap']
然后再隐藏此字段

Q2) 计算公式为, Monthly rate * discount = Net rate, 结果取整数。
我在这个表单内添加了JS代码如下:

jQuery( document ).ready(function($) {
    $('input[name="wpcf-monthly-rate"]').change(function(e){
    	var monthly_rate = $('input[name="wpcf-monthly-rate"]').val();
    	var discount = $('input[name="wpcf-discount"]').val();
        var net_rate = Math.round(monthly_rate * discount);
        $('input[name="wpcf-net-rate"]').val(net_rate)
    });
});

供你参考

Q3) 应该与Q2是同一个问题,我已经在你的网站上测试过,上面的代码工作正常,注意这只是一个例子供你参考

Q4和Q5 这两个是新问题,请开新帖,方便其他用户参考,谢谢!

#907257

第四和第五不是问题,只是帮助我说明情况,非常感谢 测试过 很不错~~~ 谢谢