我是 Javascript 的新手,我有一个非常菜鸟的问题。我在页面A中有一个复选框输入列表,一旦选择了一些复选框输入,我想将选中的值(id)传递给页面B。
在 Page B 中,将显示复选框输入列表,但如果其中一些在 Page A 中被选中,我希望它们将其隐藏。有什么最好的方法可以解决这个问题吗?
最佳答案
试试这个
---Model
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string Country { get; set; }
}-->
C#:Controller: PageA and PageB Code:
public ActionResult PageA()
{
var modelist = GetModelList(new List<Customer>());
return View(modelist);
}
public ActionResult PageB(string[] id)
{
var modelist = GetModelList(new List<Customer>());
if (id != null)
{
modelist = modelist.Where(x => id[0].Split(',').ToList().Contains(x.CustomerID)).ToList();
}
return View(modelist);
}
$(document).ready(function () {
$("#checkAll").click(function () {
$(".checkBox").prop('checked',
$(this).prop('checked'));
});
var selectedIDs1 = '';
$("#SelectId").click(function () {
var selectedIDs = new Array();
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
selectedIDs.push($(this).val());
}
});
window.location = "/home/PageB/?id=" + selectedIDs;
});
});
<body>
<h1>List of Customers Page A</h1>
<input type="button" id="SelectId" value="Selected Customers Call to Page B" />
<br />
<table border="1" cellpadding="10">
<tr>
<th>
<input type="checkbox" id="checkAll" /></th>
<th>CustomerID</th>
<th>CompanyName</th>
<th>Country</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" class="checkBox" value="@item.CustomerID" /></td>
<td>@item.CustomerID</td>
<td>@item.CompanyName</td>
<td>@item.Country</td>
</tr>
}
</table>
</body>
<body>
<h1>List of Customers Page B</h1>
<br />
<br />
<table border="1" cellpadding="10">
<tr>
<th>
<input type="checkbox" id="checkAll" /></th>
<th>CustomerID</th>
<th>CompanyName</th>
<th>Country</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" class="checkBox" value="@item.CustomerID" /></td>
<td>@item.CustomerID</td>
<td>@item.CompanyName</td>
<td>@item.Country</td>
</tr>
}
</table>
</body>
关于javascript - 使用 ajax 或 localstorage in rails 将值传递给另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51962034/