这里有一个棘手的问题 - 尝试遍历一个循环并且仅返回值等于另一个循环的值
我这里有一个问题,基本上。 。
- 我有一个循环,其中有一个站点列表,我正在浏览每个站点。
- 我有另一个循环,仅包含一些网站
- 我只想返回属性 t.title == F.title 的网站
- 如果这是真的,我想勾选一个复选框,
- 如果不是,则不要勾选复选框
问题是,它不断创建比我想要的更多的复选框,我只想要勾选匹配项的复选框 - 其余的未勾选?
foreach (Admin.DTO.SiteMap t in sites)
{
for each (Admin.DTO.SmallSites f in smallsites){
if (t.Title == f.Title)
{
myString1.Append(" <input type='checkbox' checked='yes' value='" + t.Title + "'/> ");
myString1.Append(t.Title);
}
else {
myString1.Append(" <input type='checkbox' value='" + t.Title + "'/> ");
myString1.Append(t.Title);
}
}
}
I have a problem here, Basically. .
- I have a loop which has a list of sites, im going through each and every site.
- I have another loop which only contains some sites
- I want to return only the sites where the attribute t.title == F.title
- If this is true I want to tick a check box
- if not then dont tick a check box
The problem is, it keeps creating more checkboxes than I want, I only want the ones where there are matches ticked - the rest unticked?
foreach (Admin.DTO.SiteMap t in sites)
{
for each (Admin.DTO.SmallSites f in smallsites){
if (t.Title == f.Title)
{
myString1.Append(" <input type='checkbox' checked='yes' value='" + t.Title + "'/> ");
myString1.Append(t.Title);
}
else {
myString1.Append(" <input type='checkbox' value='" + t.Title + "'/> ");
myString1.Append(t.Title);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这两个循环将创建站点 * 小站点复选框。检查了其中的一些,其余的许多都没有检查。
这是您需要的吗?
These two loop will create sites * smallsites checkboxs. With one few of it checked, many are not check in the rest.
Is this what you need?
如果您不想显示未选中的框,请删除 else 子句。
If you don't want to display unchecked boxes then remove the else clause.
您的代码将打印
smallsites
的内容 n 次 - 其中 n 是sites
中的项目数。每次打印出smallsites
时,它可能会选中其中一个框。我猜您想保持外部循环不变,但随后只需检查该站点是否包含在
smallsites
中。 pinichi 的响应将产生正确的输出,但可能效率很低。在不了解SiteMap
和SmallSites
结构的情况下,我无法提出更好的方法。Your code is going to print out the contents of
smallsites
n times - where n is the number of items insites
. Each time it prints outsmallsites
, it might check one of the boxes.I'm guessing you want to keep the outer loop as it, but then just check to see if the site is contained in
smallsites
. The reponse from pinichi will produce the correct output, but probably quite inefficiently. Without knowing the structure ofSiteMap
andSmallSites
, I can't suggest a better method.口径 - 你的逻辑在上面有缺陷。
基本上,每次进行第二个循环时,您总是会重置每个复选框,因此在前一个循环中设置的内容可能会被取消设置(反之亦然)。如前所述,删除 else 部分(或在单独的语句中预初始化值)
[编辑] - 用示例更新:
您当然可以将这两个项目中的每一个添加到新列表中(即而不是附加到 myString1) 中,然后根据需要对最终列表进行排序,然后在最后根据需要将其吐出。
干杯
Calibre - your logic is flawed above.
basically you are always resetting each checkbox everytime you go round the second loop, thus something that was set in the previous loop is likely to get unset (and vice a versa). as noted, remove the else section (or preinitialise the values in a seperate statement)
[edit] - update with example:
you could of course add each of the two items to a new list (i.e. rather than appending to myString1) as you go along and then sort the final list as you pleased and then spit that out as you wanted at the end.
cheers