如何查找所有具有匹配类名的id属性
How to find all id attr with matching class name?
我有以下HTML
,我想在<ul class="plan">...</ul>
中获取所有标签类为r_on
的无线电ID
<ul class="plan">
<li>
<p>One Month</p>
<label for="36" class="label_radio">
<input type="radio" value="300" data-name="One Month" id="36" name="long-strip">
</label>
<strong>300 $</strong>
</li>
<li>
<p>Second Month</p>
<label for="37" class="label_radio r_on">
<input type="radio" value="600" data-name="Second Month" id="37" name="long-strip">
</label>
<strong>600$</strong>
</li>
<li>
<p>Third Month</p>
<label for="38" class="label_radio r_on">
<input type="radio" checked="checked" value="1200" data-name="Third Month" id="38" name="long-strip">
</label>
<strong>1200 $</strong>
</li>
</ul>
注意:我想要用逗号分隔字符串的结果。
我想要这样的结果:37,38
知道如何用jQuery做到这一点吗?
尝试使用$.fn.map
方法:
var ids = $('.r_on :radio').map(function() {
return this.id;
}).get().join();
查看演示。
var ids = $('.r_on :radio').map(function() {
return this.id;
}).get().join();
alert(ids);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="plan">
<li>
<p>One Month</p>
<label for="36" class="label_radio">
<input type="radio" value="300" data-name="One Month" id="36" name="long-strip">
</label>
<strong>300 $</strong>
</li>
<li>
<p>Second Month</p>
<label for="37" class="label_radio r_on">
<input type="radio" value="600" data-name="Second Month" id="37" name="long-strip">
</label>
<strong>600$</strong>
</li>
<li>
<p>Third Month</p>
<label for="38" class="label_radio r_on">
<input type="radio" checked="checked" value="1200" data-name="Third Month" id="38" name="long-strip">
</label>
<strong>1200 $</strong>
</li>
</ul>
试试这个。。
<ul class="plan">
<li>
<p>One Month</p>
<label for="36" class="label_radio">
<input type="radio" value="300" data-name="One Month" id="36" name="long-strip">
</label>
<strong>300 $</strong>
</li>
<li>
<p>Second Month</p>
<label for="37" class="label_radio r_on">
<input type="radio" value="600" data-name="Second Month" id="37" name="long-strip">
</label>
<strong>600$</strong>
</li>
<li>
<p>Third Month</p>
<label for="38" class="label_radio r_on">
<input type="radio" checked="checked" value="1200" data-name="Third Month" id="38" name="long-strip">
</label>
<strong>1200 $</strong>
</li>
</ul>
<script>
$('.plan .r_on input').each(function () {
var ar = this.id;
alert(ar);
});
</script>
var ids = [];
$('.r_on :radio').each(function() {
ids.push(this.id);
});
alert(ids);
查看此演示。
var ids = [];
$('.r_on :radio').each(function() {
ids.push(this.id);
});
alert(ids);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul class="plan">
<li>
<p>One Month</p>
<label for="36" class="label_radio">
<input type="radio" value="300" data-name="One Month" id="36" name="long-strip">
</label>
<strong>300 $</strong>
</li>
<li>
<p>Second Month</p>
<label for="37" class="label_radio r_on">
<input type="radio" value="600" data-name="Second Month" id="37" name="long-strip">
</label>
<strong>600$</strong>
</li>
<li>
<p>Third Month</p>
<label for="38" class="label_radio r_on">
<input type="radio" checked="checked" value="1200" data-name="Third Month" id="38" name="long-strip">
</label>
<strong>1200 $</strong>
</li>
</ul>
相关文章:
- Javascript复选框函数:;缺少:在属性id之后"
- 语法错误:缺少:在属性 ID 之后
- 如何使用Google Embed API获取选定的帐户ID和属性ID
- 快速会话 无法设置未定义的属性“id”
- 无法读取属性id成员路由
- 如何以编程方式获取属性Id、视图Id和帐户Id
- SyntaxError:属性id后缺少:
- JSON到带有Django的JS:SyntaxError:缺少:在属性id之后
- Angular JS TypeError:无法读取路由上未定义的属性“id”
- 在 IE8 之前替换了属性(“id”)
- 无效的属性 ID
- 在为 jquery 自动完成设置源代码时获取“语法错误:缺少:在属性 ID 之后”
- 如何根据属性 ID 获取内部 html
- j查询错误 - 语法错误:缺少:在属性 ID 之后
- 缺少解析错误:在属性 ID 之后
- Jquery:检查属性 id 是否有特定的模式
- 属性ID后缺少冒号,Firebase
- Dojo确认对话框'缺少:在属性id'之后;和'找不到节点'
- jQuery)SyntaxError:缺少:在属性id之后)
- Jquery获取最近一次单击的属性ID