卡住显示从输入字段获取的我的信息

问题描述 投票:0回答:1

大家好,我被困住了,想知道是否有人可以帮助我,我是用 js 进行开发的新手,我有这段代码,我设法将它们放在一起,令人惊讶的是它可以工作。但是我遇到的问题是我收集的数据,我需要将其显示在 span 元素中。

HTML 代码 - 从输入字段 Span 元素中获取数字以显示计算出的数学结果..

<input type="text" value="" id="level" />
<span id='cash'></span>

我的代码 - 代码获取用户输入并像((2500 * 输入)* 乘数)一样计算它,代码本身的工作原理与警报中的一样,但我正在努力获取数据并将其显示在跨度框中

$(document).ready(function() {
  $("#level").keyup(function() {
    // get the users data
    let dInput = $(this).val();
    // Calculate cost based on input and hardcoded math
    let cost = ((2500 * dInput) * 1.00);
    // Show the cost in the span id?
    $('#cash').load(cost);
    alert(cost);
  });
});

已经让代码可以工作,它可以获取用户输入的数据并通过总和进行计算。

所有代码都有效,但我似乎无法让它显示跨度内收集的数据或那个..

html jquery
1个回答
1
投票

在 jQuery 中,.load() 方法用于从服务器获取数据。相反,您想使用 .text() 来更新文本内容。这是我要使用的代码。如果有效请告诉我。

$(document).ready(function() {
  $("#level").keyup(function() {
      // get the user's data
      let dInput = $(this).val();
      // Calculate cost based on input and hardcoded math
      let cost = ((2500 * dInput) * 1.00);
      // Show the cost in the span id
      $('#cash').text(cost);
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.