gpt4 book ai didi

foreach 循环内的 Javascript .match 和 .test

转载 作者:行者123 更新时间:2023-11-28 19:25:32 25 4
gpt4 key购买 nike

我已经盯着这个问题好几个小时了,我想我需要另一双眼睛。

我正在尝试在表格中查找测量值并循环查找所有毫米并将其转换为英寸。

我相信我一切正常,但在控制台中它不断抛出错误。

有人可以帮忙吗?

我这里有一个 fiddle :http://jsfiddle.net/8p6m9azd/2/

$(document).ready(function(){

$('#convert').click(function(){

//each data in table
$('td').each(function(index, value){

//find mm
if(/[mm]+/g.test(value)){
console.log('We found a mm');

//find numbers inside
numbers = value.match(/[-+]?\d*(\.(?=\d))?\d+/g);

if(numbers){
arrayLength = numbers.length;

//find and replace
for (var i = 0; i < arrayLength; i++) {
newnum = Math.ceil(numbers[i] / 0.039);
}
value = value.replace(numbers[i],newnum);
}
}
});
});
});

最佳答案

value 是一个 DOM 节点,您可能想要获取 textContent

$('td').each(function(index, value) {
// value.textContent is what you want

最简单的检查方法是:

$('td').each(function(index, value) {
value = value.textContent

关于foreach 循环内的 Javascript .match 和 .test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com