gpt4 book ai didi

javascript - 在 JavaScript 中从字符串中解析 Int

转载 作者:行者123 更新时间:2023-12-03 21:39:36 26 4
gpt4 key购买 nike

在 javascript 中,从以字母开头的字符串(例如“test[123]”)解析 INT 的最佳方法是什么?我需要一些适用于下面示例的东西。

我的JS:

$(document).ready(function() {

$(':input').change( function() {
id = parseInt($(this).attr("id")); //fails for the data below
alert(id);
});
}

我生成的 HTML:

<select id="attribute[123]">
<!-- various options -->
</select>
<select id="attribute[456]">
<!-- various options -->
</select>

谢谢!

最佳答案

您可以使用正则表达式来匹配数字:

$(this).attr("id").match(/\d+/)

关于javascript - 在 JavaScript 中从字符串中解析 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/448666/

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