查找 jQuery 每个循环内是否选中复选框的问题
我想知道这里是否有人可以帮助解决这个问题,如果选中了复选框,则将 var 值分配为 1;如果在 jQuery Each 循环内未选中,则将 var 值分配为 0。
我有一个 html 列表,如下所示:
<ul id="role-list">
<li id="role_1">
<p>Read<input type="checkbox" class="read"> Write<input type="checkbox" class="write"></p>
</li>
<li id="role_4">
<p>Read<input type="checkbox" class="read"> Write<input type="checkbox" class="write"></p>
</li>
<li id="role_5">
<p>Read<input type="checkbox" class="read"> Write<input type="checkbox" class="write"></p>
</li>
</ul>
我正在尝试使用以下代码使用 jQuery 创建多维数组:
var roles = {};
$('#role-list li').each(function() {
var role_id = jQuery(this).attr('id').replace('role_', '');
roles[role_id] = {};
$('input[type=checkbox]').each(function () {
roles[role_id][jQuery(this).attr('class')] = $(this).checked ? 1 : 0;
});
});
alert(JSON.stringify(roles));
这是我得到的输出,无论是否选中任何复选框,它都是相同的:
{"1":{"read":0,"write":0},"4":{"read":0,"write":0},"5":{"read":0,"write":0}}
我遇到的问题是该代码:
$(this).checked ? 1 : 0;
似乎无法正常工作。我已经尝试了多种变体,试图在发布之前让它发挥作用,但我仍然没有运气。
任何帮助将不胜感激。
谢谢
......
我最终决定删除复选框的每个循环并使用以下可以正常工作的代码:
var roles = {};
$('#role-list li').each(function() {
var role_id = jQuery(this).attr('id').replace('role_', '');
roles[role_id] = {};
roles[role_id]['read'] = jQuery(this).find('.read').is(':checked') ? 1 : 0;
roles[role_id]['write'] = jQuery(this).find('.write').is(':checked') ? 1 : 0
});
I was wondering whether anybody here could help with this problem I am having assigning a var value of 1 if a checkbox is checked and 0 if not checked inside a jQuery each loop.
I have a html list which looks like this:
<ul id="role-list">
<li id="role_1">
<p>Read<input type="checkbox" class="read"> Write<input type="checkbox" class="write"></p>
</li>
<li id="role_4">
<p>Read<input type="checkbox" class="read"> Write<input type="checkbox" class="write"></p>
</li>
<li id="role_5">
<p>Read<input type="checkbox" class="read"> Write<input type="checkbox" class="write"></p>
</li>
</ul>
I am trying to create a multidimensional array with jQuery using the following code:
var roles = {};
$('#role-list li').each(function() {
var role_id = jQuery(this).attr('id').replace('role_', '');
roles[role_id] = {};
$('input[type=checkbox]').each(function () {
roles[role_id][jQuery(this).attr('class')] = $(this).checked ? 1 : 0;
});
});
alert(JSON.stringify(roles));
Here is the output I am getting which is the same whether any check boxes are checked or not:
{"1":{"read":0,"write":0},"4":{"read":0,"write":0},"5":{"read":0,"write":0}}
The problem I am getting is that the code:
$(this).checked ? 1 : 0;
does not seem to work correctly. I have tried multiple variations to try and get this to work before posting but I am still having no luck.
Any help would be appreciated.
Thanks
.............
I eventually decided to remove the each loop for the checkboxes and use the following code which works correctly:
var roles = {};
$('#role-list li').each(function() {
var role_id = jQuery(this).attr('id').replace('role_', '');
roles[role_id] = {};
roles[role_id]['read'] = jQuery(this).find('.read').is(':checked') ? 1 : 0;
roles[role_id]['write'] = jQuery(this).find('.write').is(':checked') ? 1 : 0
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要
或将查询更改为:
在这种情况下,您根本不需要三元,这可能是更好的选择,因为您没有用零填充角色(您可能想要?)
$(this) .checked
不存在,因为$(this)
是 jquery 对象,而.checked
是单个元素的属性。you want
or change your query to:
in which case you don't need the ternary at all, which may be the better option since you're not filling roles with zeros (which you may want?)
$(this).checked
doesn't exist because$(this)
is a jquery object and.checked
is an attribute of the individual element.