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

如何将jQuery获取的totalTax值赋值到HTML输入框?

实现方法

先基于常见的JSON结构、HTML输入框代码,结合你的需求给出具体实现方案:

示例JSON返回结构

{
  "lineItems": [
    { "totalTax": 12.5 },
    { "totalTax": 8.7 }
  ]
}

示例HTML输入框代码

<input type="text" id="taxInput1" placeholder="第一个税费">
<input type="text" id="taxInput2" placeholder="第二个税费">

修改后的jQuery AJAX代码

在success回调中,直接提取lineItems里的对应值赋值给输入框即可,同时保留原有的控制台打印逻辑:

$.ajax({
  url: '你的请求接口地址',
  type: 'GET',
  dataType: 'json',
  success: function(response) {
    // 遍历lineItems并打印totalTax到控制台
    response.lineItems.forEach(item => {
      console.log(item.totalTax);
    });

    // 将前两个totalTax分别赋值给输入框
    if (response.lineItems.length >= 1) {
      $('#taxInput1').val(response.lineItems[0].totalTax);
    }
    if (response.lineItems.length >= 2) {
      $('#taxInput2').val(response.lineItems[1].totalTax);
    }
  },
  error: function(xhr, status, error) {
    console.error('请求失败:', error);
  }
});

补充说明

  • 如果lineItems的长度超过2,超出的totalTax只会打印到控制台,不会赋值给输入框,你可以根据实际需求调整逻辑
  • 也可以结合遍历索引来赋值,适合需要动态匹配多个输入框的场景:
    response.lineItems.forEach((item, index) => {
      console.log(item.totalTax);
      if (index === 0) $('#taxInput1').val(item.totalTax);
      if (index === 1) $('#taxInput2').val(item.totalTax);
    });
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 11:52:11