如何根据 json 数据列表更新 html 项目列表
我的 html 页面中有一个复选框列表,如下所示:
<ul id="myList">
<li class="checkboxItem">
<input type="checkbox" name="item1" value="001" id="item-1"/>
<label for="item-1" class="checkboxLabel">Display 1</label>
</li>
<li class="checkboxItem">
<input type="checkbox" name="item2" value="042" id="item-2"/>
<label for="item-2" class="checkboxLabel">Display 42</label>
</li>
</ul>
现在我调用以获取一些 json 数据,返回结果如下:
[{"name":"002","title":"Display 1"}]
我想要做的是循环返回的 json 并更新复选框列表,以便任何不在返回列表中的项目将被禁用,并且标题与给定标签匹配的项目将更新输入值。
所以在这个例子中,item2将被禁用,item1的值将更新为002。
这是我到目前为止所拥有的,我不太确定从这里开始到哪里,也就是说,在循环内做什么。我确实可以控制 json 的返回方式,因此如果以其他格式返回 json 有意义,我可以这样做。
编辑,更新了功能,见下文。然而,一旦我进入每个函数内的 for 循环,elem 就会得到一个值“0”而不是一个 js 对象,例如: {"name":"002","title":"显示 1"}。显然,数据正在从函数的外部范围传输到每个函数的内部范围,但我该如何实现这一点呢?
function(data) {
$('#myList').children('li').each(function(i,e) {
for(var elem in data) {
var elemDescr = elem['title'];
var elemName = elem['name'];
if(elemDescr==$(this).find('.checkboxLabel').text()) {
$(this).find('input').attr('value',elemName);
}
}
});
I have a list of checkboxes in my html page, like so:
<ul id="myList">
<li class="checkboxItem">
<input type="checkbox" name="item1" value="001" id="item-1"/>
<label for="item-1" class="checkboxLabel">Display 1</label>
</li>
<li class="checkboxItem">
<input type="checkbox" name="item2" value="042" id="item-2"/>
<label for="item-2" class="checkboxLabel">Display 42</label>
</li>
</ul>
now I make a call to get some json data, which comes back like so:
[{"name":"002","title":"Display 1"}]
what I want to do is loop the returned json and update the list of checkboxes such that any item not in the returned list is disabled, and those where the title matches a given label, the input value is updated.
so in this example, item2 will be disables and item1 will have its value updates to 002.
here's what I have so far, i'm not quite sure where to go from here, that is, what to do inside the loop. I do have some control over how the json is returned, so if it makes sense to retunr the json in another format, I can do that.
EDIT, updated the function, see below. however, once I get inside the for loop inside the each function, elem is getting a value of "0" rather than a js object such as:
{"name":"002","title":"Display 1"}. clearly data, is being transferred from the outside scope of the function to the inside scope of the each function, but how do I make that happen?
function(data) {
$('#myList').children('li').each(function(i,e) {
for(var elem in data) {
var elemDescr = elem['title'];
var elemName = elem['name'];
if(elemDescr==$(this).find('.checkboxLabel').text()) {
$(this).find('input').attr('value',elemName);
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了我的问题。而不是做:
我需要做:
I have found my problem. rather than doing:
I needed to do:
为每个复选框设置一个外部循环可能会更容易,并且内部循环遍历每个 json 元素,根据元素/复选框是否匹配来启用或禁用。
因此,您所拥有的反转:
另一个选项(可能不太理想,因为它可能会导致多个禁用/启用的转换)是禁用所有复选框,并在循环访问每个元素时启用它们。
It might be easier to have an outer loop for each checkbox, and an inner loop go through each json element, enabling or disabling based on whether the element/checkboxes have a match.
So an inversion of what you have:
Another option (probably less desirable because it may cause multiple disabled/enabled transitions) is to disable all checkboxes, and enable them as you loop through each element.