首页 > 解决方案 > 如何从div中获取一个数字然后相乘

问题描述

我的结果如下 显示 50 个条目中的 5 到 40 个我需要获得总条目忽略“显示 5 到 40 个”并保持数字“50”将其插入输入总数然后乘以 10

<div class="info" id="info">Showing 30 to 40 of 50 entries</div>

<input type="text" id="total" name="total">
<input type="text" id="total" name="x10">

标签: javascriptjquery

解决方案


这个答案强烈假设文本始终显示“显示 50 个条目中的 5 到 40 个”或类似文本,您还需要更改其中一个框的 id,因为所有 id 都应该是唯一的才能使用在js代码中

// use js to get the value
const info = document.getElementById("info").innerHTML
// get an array of text seperated by space
const texts = info.split(" ")
// get the 2nd last character
const number = parseInt(texts[texts.length - 2])

// put those values into the input boxes
document.getElementById("total").value = number
document.getElementById("totalTimes10").value = number*10
<div class="info" id="info">Showing 30 to 40 of 50 entries</div>

<input type="text" id="total" name="total">
<input type="text" id="totalTimes10" name="x10">


推荐阅读