Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript text()获取html价格并使用数字字段增加价格_Javascript_Php_Jquery_Html_Wordpress - Fatal编程技术网

Javascript text()获取html价格并使用数字字段增加价格

Javascript text()获取html价格并使用数字字段增加价格,javascript,php,jquery,html,wordpress,Javascript,Php,Jquery,Html,Wordpress,我怎样才能得到数字价格?简单的产品有: <td class="wh-price"> <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>425.00</span> </td>

我怎样才能得到数字价格?简单的产品有:

<td class="wh-price">                           
   <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>425.00</span>                       
</td>
    <td class="wh-price">                           
        <del class="original-computed-price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>625.00</span></del>

        <span style="display: block;" class="wholesale_price_container">
            <span class="wholesale_price_title"></span>
            <ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>54,321.00</span></ins>
        </span>
    </td>
但它不起作用,大部分时间都显示
$NaN
所以我尝试了
const originalPrice=parseInt($el.find('.woocommerce Price amount.amount').text(),10)

仍然不起作用。在我看来,
.text()
正在得到
$
,这会阻止解析


html代码是用php
生成的;对于简单的产品,我只能使用
获得没有标记的数值,但是对于批发产品价格,使用简单的php是不可能的。

您可以使用
replace()
函数这样做:

const originalPriceStr = $el.find('.woocommerce-Price-amount.amount').text().replace(/[$,]/g, ''));
const originalPrice = parseFloat(originalPriceStr, 10);

此外,您可能希望使用
parseFloat
,如果有美分,您不希望创建舍入错误。

您可以使用
replace()
函数执行此操作:

const originalPriceStr = $el.find('.woocommerce-Price-amount.amount').text().replace(/[$,]/g, ''));
const originalPrice = parseFloat(originalPriceStr, 10);
此外,您可能希望使用
parseFloat
,以防出现美分,您不希望创建舍入错误。

如果愿意,您可以执行.text().replace(),但只有在您知道要尝试执行的货币符号的情况下,该操作才会起作用。replace()

我会在价格周围添加一个
425.00
,然后选择它
$el.find(“[data role price]”)。text()

这只是我个人对如何解决这个问题的偏好,尽管它添加了一些额外的HTML,但好消息是它与货币无关

如果您不能修改HTML,那么您可以这样做,这也是货币不可知的

const currencySymbol = $el.find('.woocommerce-Price-currencySymbol');
const originalPriceWithSymbol = $el.find('.woocommerce-Price-amount.amount').text();
const originalPriceWithoutSymbol = originalPriceWithSymbol.replace(currencySymbol, '');
如果愿意,可以执行.text().replace(),但只有知道要替换的货币符号时,该操作才会起作用

我会在价格周围添加一个
425.00
,然后选择它
$el.find(“[data role price]”)。text()

这只是我个人对如何解决这个问题的偏好,尽管它添加了一些额外的HTML,但好消息是它与货币无关

如果您不能修改HTML,那么您可以这样做,这也是货币不可知的

const currencySymbol = $el.find('.woocommerce-Price-currencySymbol');
const originalPriceWithSymbol = $el.find('.woocommerce-Price-amount.amount').text();
const originalPriceWithoutSymbol = originalPriceWithSymbol.replace(currencySymbol, '');

我认为最好混合使用jquery和本机函数。
JQuery自动从结果集合中筛选出文本节点,但使用

$el.find('.woocommerce-Price-amount.amount').get()[0].childNodes[1].textContent
您可以为您的值获取精确的文本节点。

它支持所有货币,不需要更改标记。

我认为最好混合使用jquery和本机函数。
JQuery自动从结果集合中筛选出文本节点,但使用

$el.find('.woocommerce-Price-amount.amount').get()[0].childNodes[1].textContent
您可以为您的值获取精确的文本节点。

它支持所有货币,并且不需要更改标记。

为简单起见,您需要对货币进行分析,以获得如下文本结果:

..text().match(/([0-9]*\.[0-9]{2}|[0-9]+\.[0-9]{2}|[0-9]+)/)[0];

为了简单起见,您需要解析文本结果的money,如下所示:

..text().match(/([0-9]*\.[0-9]{2}|[0-9]+\.[0-9]{2}|[0-9]+)/)[0];

您可以使用
.substring(1)

var text=$('.woocommerce Price amount.amount').text().substring(1);
log(parseFloat(text.toFixed(2))


$425.00
您可以使用
.substring(1)

var text=$('.woocommerce Price amount.amount').text().substring(1);
log(parseFloat(text.toFixed(2))


425.00美元
谢谢你们,从你们的回答中我想出了我自己的代码。这对我来说非常有效

    $(document).ready(function(){
      $(".qty").on('change',function() {
        setPriceQ();
      });
    })

    const setPriceQ = function(){
      $.each(
        $('tr[data-role="product"]'),
        function(index,element){
          const $el = $(element);

          const originalPriceStr = $el.find('.wh-price .woocommerce-Price-amount.amount').text().replace(/[^\d\.\-]/g, "");
          const originalPrice = parseFloat(originalPriceStr, 10);

          const wholesalePriceStr = $el.find('.wh-price .wholesale_price_container .woocommerce-Price-amount.amount').text().replace(/[^\d\.\-]/g, "");
          const wholesalePrice = parseFloat(wholesalePriceStr, 10);   

          const totalQuantity = $el.find(".qty").val();

          if($el.find('.wholesale_price_container').length){
              $el.find('#result').html( '$' + ( wholesalePrice * totalQuantity ) .toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1," ) );
           }
          else {
              $el.find('#result').html( '$' + ( originalPrice * totalQuantity ) .toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1," ) );    
           }
          }
        );
      };    

谢谢你们,从你们的回答中我想出了我自己的代码。这对我来说非常有效

    $(document).ready(function(){
      $(".qty").on('change',function() {
        setPriceQ();
      });
    })

    const setPriceQ = function(){
      $.each(
        $('tr[data-role="product"]'),
        function(index,element){
          const $el = $(element);

          const originalPriceStr = $el.find('.wh-price .woocommerce-Price-amount.amount').text().replace(/[^\d\.\-]/g, "");
          const originalPrice = parseFloat(originalPriceStr, 10);

          const wholesalePriceStr = $el.find('.wh-price .wholesale_price_container .woocommerce-Price-amount.amount').text().replace(/[^\d\.\-]/g, "");
          const wholesalePrice = parseFloat(wholesalePriceStr, 10);   

          const totalQuantity = $el.find(".qty").val();

          if($el.find('.wholesale_price_container').length){
              $el.find('#result').html( '$' + ( wholesalePrice * totalQuantity ) .toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1," ) );
           }
          else {
              $el.find('#result').html( '$' + ( originalPrice * totalQuantity ) .toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1," ) );    
           }
          }
        );
      };    

使用它可以显示完整的价格$2000.00。$符号是子节点,使用此
$el.find('.woocommerce Price amount.amount').get()[0]。子节点[0]。textContent
仅显示
$
。我需要数值来计算。因此
$el.find('.woocommerce Price amount.amount').get()[0]。childN‌​odes[0]。textContent
显示
$
,但
$el.find('.woocommerce Price amount.amount').get()[0]。childN‌​odes[1]。文本内容显示2000.00美元??你的标记中有两个$符号吗?你在用什么来测试它?我在上面的示例中没有看到$2000.00。在这里,检查控制台输出。我用过你的标记<代码>常量woo=$el.find('.woocommerce Price amount.amount').get()[0]。childNodes[1]。textContent
$el.find('.resultWithWoo').html(woo*10)我想将其与数量编号字段相乘,使用它显示完整价格$2000.00。$符号是子节点,使用此
$el.find('.woocommerce Price amount.amount').get()[0]。子节点[0]。textContent
仅显示
$
。我需要数值来计算。因此
$el.find('.woocommerce Price amount.amount').get()[0]。childN‌​odes[0]。textContent
显示
$
,但
$el.find('.woocommerce Price amount.amount').get()[0]。childN‌​odes[1]。文本内容显示2000.00美元??你的标记中有两个$符号吗?你在用什么来测试它?我在上面的示例中没有看到$2000.00。在这里,检查控制台输出。我用过你的标记<代码>常量woo=$el.find('.woocommerce Price amount.amount').get()[0]。childNodes[1]。textContent
$el.find('.resultWithWoo').html(woo*10)我想将其与quantity number字段相乘。您可以共享源文本,以便我可以测试正则表达式吗?我将修复所有错误并更新答案。请查看我发布的答案,目前这只是问题所在。您能否共享源文本,以便我可以测试正则表达式?我将修复所有错误并更新答案。请查看我发布的答案,目前这只是问题所在。我已修复了我的答案,现在可以使用了。我将其更改为
replace(/[$,]/g,,)
,以便它使用“正则表达式”匹配
$
。我修复了我的答案,它现在可以工作了。我将其更改为
replace(/[$,]/g,,)
,以便使用“正则表达式”匹配
$
。更新了我的原始问题,并用curren发布了答案