【问题标题】:how to convert javaScript array into string And store in Hidden field in MVC?如何将javaScript数组转换为字符串并存储在MVC的隐藏字段中?
【发布时间】:2017-09-25 05:02:36
【问题描述】:

点击功能:-

    //$(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. 当我单击分页时,隐藏字段值应该加载到数组中,然后在数组中它还会存储新检查的值和隐藏字段中的现有值

【问题讨论】:

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


    【解决方案1】:

    样本

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

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

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

    使用 jquery 可以设置值。

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

    【讨论】:

    • 更简洁的方法是使用.join(),因为toString() 是Object 的一部分,可以用于javascript 中的所有对象。如果您只想接收数组,请使用前者。
    猜你喜欢
    • 2018-07-04
    • 1970-01-01
    • 2015-11-11
    • 2022-07-26
    • 2015-05-18
    • 2015-10-20
    • 2022-11-29
    • 2015-10-26
    • 1970-01-01
    相关资源
    最近更新 更多