You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在payModal函数中使用全局变量finalPrice的动态值

问题描述

我之前通过创建新变量赋值给finalPrice解决了问题,但想找其他实现方式。现在希望访问add_to_download_entries函数上方定义的全局变量finalPrice,用它的动态值替换payModal函数里finalPrice = 44;中的固定值44。具体来说,当document.ready中执行finalPrice = "Pay Now $" + Number(counts) * Number(22);完成赋值后,这个动态值要替换掉payModal里的44,该怎么实现?

相关代码
<script>   
 // Pay Modal 
    async function payModal() {
      
      await fetch("{{ route('paymentModal') }}")
      .then(response => response.json())
        .then(response => {
          const form = document.getElementById('payment-form');
          _key_for_ss = response.paymentIntent.client_secret;
          paymentIntentID = response.paymentIntent.id;
          finalPrice = 44; 
          async function openModal() {
            await createCard();
            $('#pay-modal').modal('show');
          }
          openModal();
        })
    }
    
    var selected_lostwill_array = [];
    
     var counts = 1;
     var checkboxes;
     var finalPrice;
    function add_to_download_entries(data) {
       checkboxes = document.querySelectorAll('input[name="checkWills"]:checked');
        counts = checkboxes.length;
      if($.inArray(data, selected_lostwill_array) >= 0){
        selected_lostwill_array = $.grep(selected_lostwill_array, function(value){
             return value != data;
           });
         }else{
          selected_lostwill_array.push(data);
         }
         // DataTables Functions 
    $(document).ready( function () {
        $('#org_lost_table').DataTable();
        $('#all_lost_table').DataTable();
        if(counts>0){
             finalPrice = "Pay Now $" + Number(counts) * Number(22);
            
            $( "#payment-sub-btn" ).text(finalPrice);
           }else $( "#payment-sub-btn" ).text(0); 
    } );
    }
</script>
解决方案

方式一:拆分全局变量,分离金额数值与显示文本

目前你把金额和"Pay Now $"拼接后存在finalPrice里,但payModal需要的是纯数值(比如44是2*22的结果),建议新增全局变量存储纯金额:

  1. 在全局声明新增变量:
var counts = 1;
var checkboxes;
var finalPrice;
var finalPriceAmount; // 新增:存储纯金额数值
  1. 在document.ready中同时赋值两个变量:
if(counts>0){
  finalPriceAmount = Number(counts) * Number(22); // 存纯数值
  finalPrice = "Pay Now $" + finalPriceAmount; // 存显示文本
  $( "#payment-sub-btn" ).text(finalPrice);
}else{
  finalPriceAmount = 0;
  $( "#payment-sub-btn" ).text(0); 
}
  1. 在payModal里直接使用纯数值变量:
// 替换原有的finalPrice = 44;
finalPrice = finalPriceAmount;
// 若后续逻辑仅需数值,直接用finalPriceAmount即可,无需再赋值给finalPrice

方式二:从现有finalPrice字符串提取数值

如果不想新增变量,可以从已有的finalPrice字符串中提取金额数值:
在payModal里替换finalPrice = 44;为:

// 提取字符串中的数字部分并转为数值
const priceAmount = Number(finalPrice.replace(/[^0-9.]/g, ''));
finalPrice = priceAmount;

注意:该方式依赖finalPrice格式固定为"Pay Now $X",若后续文本格式变更,需同步调整正则表达式。

额外注意事项

  • 确保payModal执行时,finalPrice已完成赋值(即document.ready和add_to_download_entries已执行),否则会拿到undefined。可在payModal中添加判断:
if (typeof finalPrice === 'undefined' || finalPrice === 0) {
  // 处理未选择任何项的情况,比如提示用户选择后再操作
  return;
}

内容的提问来源于stack exchange,提问作者user022yufjb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 00:15:02