简体   繁体   English

使用 JavaScript (jQuery) 创建自定义计算

[英]Creating custom calculations using JavaScript (jQuery)

I wish to take the value of an HTML string contained within <bdi> tags - perform a calculation on it - and output the results in a separate <bdi> string, dependent on which input is selected on-page.我希望获取包含在<bdi>标记中的 HTML 字符串的值 - 对其执行计算 - 并将 output 结果生成一个单独的<bdi>字符串,具体取决于在页面上选择的输入。

The source <bdi> value changes dynamically based on user interaction, but I want to know if what I'm asking is feasible and a rough guide on how to achieve it?<bdi>值根据用户交互动态变化,但我想知道我所问的是否可行以及如何实现它的粗略指南?

Screenshot to illustrate the user elements:用于说明用户元素的屏幕截图:用于说明用户元素的屏幕截图

In the DOM, the source value is nested in the below tag:在 DOM 中,源值嵌套在以下标记中:

 <li class="grand_total"> <span class="name">Grand Total</span> <span class="price"> <span class="woocommerce-Price-amount amount"> <bdi> <span class="woocommerce-Price-currencySymbol">£</span> 301.00 </bdi> </span> </span> </li>

If the deposit input option is selected, how can I target this value (or string - sorry if incorrect terminology) using jQuery to divide it by 100 and multiply by 20 - and then output the resultant figure in the target which is nested as follows:如果选择了存款输入选项,我如何使用 jQuery 将其除以 100 并乘以 20 - 然后 output 目标中的结果图形嵌套如下:

 <li class="deposit_free_total"> <span class="name">Due Today</span> <span class="price"> <span class="woocommerce-Price-amount amount"> <bdi> <span class="woocommerce-Price-currencySymbol">£</span>301.00 </bdi> </span> </span> </li>

The input which needs to be selected to initiate the above has an id of #wc-option-pay-deposit.需要选择以启动上述输入的 ID 为#wc-option-pay-deposit。 I'm not sure where to start, so any help is appreciated!我不知道从哪里开始,所以任何帮助表示赞赏!

PROGRESS UPDATE:进展更新:

I've devised the following code, which works as expected:我设计了以下代码,它按预期工作:

 <script> jQuery(document).ready(function(changeToPay){ if (jQuery('#wc-option-pay-deposit').is(':checked')) { (jQuery("h1").html("Test")); } }) jQuery(document).ready(function(){ changeToPay(); }); </script>

Seems like the next step would be to create variables, so that data can be acquired and calculations can be performed, but I've no idea how to join up the dots...似乎下一步是创建变量,以便可以获取数据并执行计算,但我不知道如何连接这些点......

Given you only want to change what is displayed on the client-side...鉴于您只想更改客户端显示的内容...

Try this... And if it works as expected, I will edit with explanations.试试这个......如果它按预期工作,我将编辑解释。

jQuery(document).ready(function(changeToPay){

  setInterval(function(){
    if (jQuery('#wc-option-pay-deposit').is(':checked') && !jQuery(".deposit_free_total bdi").is(".calculated")) {
      jQuery(".deposit_free_total bdi").html(jQuery(".grand_total .woocommerce-Price-currencySymbol")[0].outerHTML + (parseFloat(jQuery(".grand_total bdi").text().replace("£","").trim())*0.2).toFixed(2)).addClass("calculated")
    } 
  },50)
  
})

EDIT for GB number formatting over a thousand:编辑超过一千的 GB 数字格式:

You have to remove the thousand separator coma... Easy using .replace() .您必须删除千位分隔符逗号...易于使用.replace()
But you have to re-add it if the 20% result still is over a thousand!但是如果20%的结果仍然超过一千,你必须重新添加它! You need .toLocaleString() with specific options.您需要具有特定选项的.toLocaleString() I admit that would not have been easy to find.我承认这不容易找到。

So the statement inside the if condition now is:所以现在if条件里面的语句是:

jQuery(".deposit_free_total bdi").html(jQuery(".grand_total .woocommerce-Price-currencySymbol")[0].outerHTML + (parseFloat(jQuery(".grand_total bdi").text().replace("£","").replace(",","").trim())*0.2).toLocaleString('en-GB',{minimumFractionDigits:2, maximumFractionDigits:2})).addClass("calculated")

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

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