javascript - 使用 ajax 或 localstorage in rails 将值传递给另一个页面

标签 javascript

我是 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/

相关文章:

javascript - 使用 Highcharts 在折线图中的 2 点之间画一条线?

javascript - Recurly JS v4 - 访问控制允许来源

javascript - 带偏移量的哈希位置

javascript - 从带有空格的数组加载选择列表

javascript - 在 Chrome/IE11/Firefox 中的隐藏空字段中设置值

javascript - 仅在向上滚动时固定导航栏

javascript - 当我使用 css 使其响应时,Html Canvas 变得模糊

javascript - 如何在jQuery中计算div的高度?

javascript - Canvas 数据到图像

javascript - 为什么在这种特殊情况下 View 没有更新? ($scope.$apply)