javascript - 如何将javaScript数组转换为字符串并存储在MVC中的隐藏字段中?

标签 javascript jquery arrays model-view-controller hidden-field

onclick 函数:-

    //$(document).ready(function() {
AddRemoveCustomer = function(){
    var CustomerIDArray =[];
    $(".checkBoxClass").click(function(e) {
        var arr = CustomerIDArray;
        var checkedId =$(this).attr('id');
        if ($(this).prop('checked')){
            CustomerIDArray.push(checkedId);
            arr = CustomerIDArray;
        }
        else
        {
            jQuery.each(CustomerIDArray, function(i, item){
                if (arr[i] == checkedId)
                {
                    arr.splice(i, 1);
                }
            });
            CustomerIDArray = arr;
        }
        var ids = "";
        jQuery.each(CustomerIDArray, function(i, item){
            if (ids == "")
            {
                ids = CustomerIDArray[i];
            }
            else
            {
                ids = ids + "," + CustomerIDArray[i];
            }
        });
        alert(ids);
    });;
    };
    </script>

查看:-

<table id="tblEmailScheduler"  class="table-bordered col-offset-12">
            <thead>
                <tr class="label-primary">
                    <th style="padding:5px 15px;">
                        First Name
                    </th>
                    <th style="padding:5px 15px;">
                        Last Name
                    </th>
                    <th style="padding:5px 15px;">
                        Email ID
                    </th>
                    <th style="padding:5px 15px;">
                        Customer Type
                    </th>
                    <th style="padding:5px 15px;">
                        Customer Designation
                        @Html.DropDownList("CustomerDesignation", new SelectList(ViewBag.SelectAllCustomerDesignationDDL, "Value", "Text"), new { id = "CustomerDesignationDDL" , name = "CustomerDesignationDDL" })
                    </th>
                    <th style="padding:5px 15px;">
                        Select All
                        <div class="checkbox control-group">
                            <label>
                                <input type="checkbox" id="cbSelectAll" />
                            </label>
                        </div>
                    </th>

                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th colspan="2">
                        EmailTemplate :
                        @Html.DropDownList("EmailSubject", new SelectList(ViewBag.SelectAllEmailTemplateDDL, "Value", "Text"), new { id = "SelectAllEmailTemplateDDL" })
                    </th>
                    <th colspan="2">
                        Set Date And Time:
                        <input type="text" class = "from-date-picker" readonly = "readonly"  />
                    </th>
                    <th colspan="2">
                       <input type="submit" value="Schedule" id="btnSubmit" class="btn btn-default" />
                    </th>
                    <td>

                    </td>
                </tr>
            </tfoot>
            @foreach (var item in Model)
            {
                <tr style="text-align:center">
                    <td id="tblFirstName">
                        @item.FirstName
                    </td>
                    <td id="tblLastName">
                        @item.LastName
                    </td>
                    <td id="tblEmailID">
                        @item.EmailID
                    </td>
                    <td id="tblCustomerType">
                        @item.CustomerType
                    </td>
                    <td id="tblCustomerDesignation">
                        @item.CustomerDesignation
                    </td>
                    <td>
                        <div class="checkbox control-group">
                            <label>
                                <input type="checkbox" id="@item.CustomerID" value="@item.CustomerID"  onclick="AddRemoveCustomer()" class="checkBoxClass"/>
                                @*@Html.CheckBox("Select", new { id = "cbCustomer", item.CustomerID})*@
                            </label>
                        </div>
                    </td>
                </tr>
            }
      </table>
        <input type="hidden" id="hfCustomerID"/>
  1. 当我检查 JavaScript 中存储在数组中的行的复选框值时,我给出了复选框。
  2. 我想将 JavaScript 数组转换为字符串,并且它应该保存在隐藏字段中。
  3. 当我单击分页时,隐藏字段值应加载到数组中,然后在数组中,它还会将新的选中值与隐藏字段中的现有值一起存储

最佳答案

示例

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var value=fruits.toString();

在 html 页面中声明一个输入。

<input type="hidden" id="hidden" class="btn btn-default" />

使用jquery你可以设置值。

 $('#hidden').val(value);

关于javascript - 如何将javaScript数组转换为字符串并存储在MVC中的隐藏字段中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657127/

相关文章:

javascript - insertAdjacentHTML 有问题吗?

javascript - 为什么元标记注入(inject)不适用于桌面浏览器?

javascript - 将简单的 jquery 代码转换为 javascript

c++求和使用for循环和二维数组

c - 添加要在函数中操作的大型数组时内存不足

javascript - 从扩展中执行小书签是否安全?

javascript - 运行 setInterval 函数,停止 3 秒然后继续运行

javascript - 对字符串进行编码以便通过 HTTP 请求发送?

php - 加载使用 jQuery 滚动动态创建的内容

c++ - 通过地址访问 std::array 数据是否安全?