javascript - 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)。
有人可以告诉我,我做错了什么吗?谢谢!
解决方案
除了这个小错误,你做得很好
ico1helper.split('numbercount="')[0].split('"')[1].trim()
^ ^
索引必须为 1 & 0 才能工作
推荐阅读
- python-2.7 - python在源代码/转发引用的末尾“隐藏”一个大文字?
- python - 如何使用python登录网站并抓取数据
- r - 使用 ebal 创建余额表时 t 检验和 KS 检验 pvalue 为 0
- python-3.x - 在 VS 代码中调试 Python 不起作用
- c - 使用指针调用函数时出现错误“在 '&' 标记之前出现预期的 ')'
- azure - 天蓝色的批处理
- ruby-on-rails - 如何在从控制器返回之前在原始记录列表之前添加项目列表?
- html - 锚点不起作用(转到 # 但不移动)
- javascript - .checked 不会更改属性 css 状态
- html - 使用 API 时 Browsersync 不刷新更新?