Wic*_*wok 22 javascript jquery
在javascript中,从以字母开头的字符串(例如"test [123]")解析INT的最佳方法是什么?我需要一些适合下面例子的东西.
我的JS:
$(document).ready(function() {
$(':input').change( function() {
id = parseInt($(this).attr("id")); //fails for the data below
alert(id);
});
}
Run Code Online (Sandbox Code Playgroud)
我生成的HTML:
<select id="attribute[123]">
<!-- various options -->
</select>
<select id="attribute[456]">
<!-- various options -->
</select>
Run Code Online (Sandbox Code Playgroud)
谢谢!
Gum*_*mbo 57
您可以使用正则表达式来匹配数字:
$(this).attr("id").match(/\d+/)
Run Code Online (Sandbox Code Playgroud)
Joe*_*orn 24
parseInt(input.replace(/[^0-9-]/,""),10)
Run Code Online (Sandbox Code Playgroud)