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

how to convert javaScript array into string And store in Hidden field in MVC?

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);