本文介绍了如何将javascript数组数据导入MVC中的隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本: -



Script:-

<pre><script type="text/javascript">
    AddRemoveCustomer = function(){
        var CustomerIDArray =[];
        var hidden = document.getElementById("hfCustomerID"); 
        $(".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;
            }
            //$('#hfCustomerID').val(JSON.stringify(checkedId)); //store array
            //var value = $('#hfCustomerID').val(); //retrieve array
            //alert(value);
            var value = CustomerIDArray.toString();
            $('#hfCustomerID').val(value);
            alert(value);

            var ids = "";
            jQuery.each(CustomerIDArray, function(i, item){
                if (ids == "")
                {
                    ids = CustomerIDArray[i];
                }
                else
                {
                    ids = ids + "," + CustomerIDArray[i];
                }
            });
        });;
        };
        </script>





查看: -





View:-

<pre><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.在表中使用复选框作为复选框选中行ID在JavaScript中进入数组。

2.我想将数组值转换为字符串并希望存储在隐藏字段中。

3.当我点击下一页时,数组应从隐藏字段加载现有值,然后应该从表中获取选中的值并将其存储在隐藏的字段中。



我尝试过的方法:



i尝试.tostring将数组转换为字符串,并尝试加载到隐藏字段




1.In Table used checkbox as checkbox checked row ID goes Into array in JavaScript.
2.I want to convert array value into string and want to store in hidden field.
3.When i Click on next page array should load existing value values from hidden field and then should get checked values from table and store them in hidden field.

What I have tried:

i tried .tostring to convert array into string and the try to load into hidden field

推荐答案




这篇关于如何将javascript数组数据导入MVC中的隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:01