首页 > 解决方案 > JS:从字符串中获取值

问题描述

我正在寻找一种在文本中搜索字符串并获取其背后的值的方法。对不起,我的JS很差。

我正在使用的文本:

<label for="sample-group-15822160-0"><br>
<input name="sample-group-15822160" id="sample-group-15822160-0" value="option-1" numbercount="1" type="radio" checked="checked">Option 1</label>

我需要什么:1

-> 所以只是“。

我尝试了什么:

var numhelper = sampleButtonObjects[index].outerHTML;
    var numhelperNum = ico1helper.split('numbercount="')[0].split('"')[1].trim();
                    var num = numhelperNum ;

我收到的是:sample-group-15822160-0(ID)。

有人可以告诉我,我做错了什么吗?谢谢!

标签: javascriptsplit

解决方案


除了这个小错误,你做得很好

ico1helper.split('numbercount="')[0].split('"')[1].trim()
                                  ^             ^

索引必须为 1 & 0 才能工作


推荐阅读