本文介绍了jQuery + parseInt()不能很好地播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个jQuery片段用于从输入字段获取ID号
I have this snippet of jQuery used to get an ID number from an input field
$('table th input').change(function() {
var id = $(this).attr('id');
id = parseInt(id);
id = isNaN(id) ? 0 : id;
alert(id);
});
字段的ID是'col2Name'等等,我想抓住那里的2,由于某种原因,在我的警报中我总是得到0,现在当我尝试做的时候:
the ID's of the fields are along the lines of 'col2Name' etc, and I want to just grab the 2 from there, for some reason in my alert i am always getting 0, now when i try to just do:
alert(parseInt('12978sdkjfhakj'));
我收到12978的相应回复,为什么这不起作用?
I get the appropriate response of 12978, why is this not working?
推荐答案
parseInt
函数始终从字符串的左侧开始。试试这个:
The parseInt
function always starts from the left side of the string. Try this:
var i = parseInt(yourString.replace(/\D/g, ''), 10);
这篇关于jQuery + parseInt()不能很好地播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!