Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Script:-

JavaScript
<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:-

HTML
<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.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
Posted
Updated 27-Apr-17 19:50pm

1 solution

<div>
   
    <table id="tblEmailScheduler" class="table-bordered col-offset-12">
        
        <tr style="text-align: center">
            <td>
              checkbox 1:  <input type="checkbox" id="1" value="11"  class="checkBoxClass"/>
            </td>
        </tr>
        
        <tr style="text-align: center">
            <td>
               checkbox 2: <input type="checkbox" id="2" value="12" class="checkBoxClass"/>
            </td>
        </tr>
        <tr style="text-align: center">
            <td>
               checkbox 3: <input type="checkbox" id="3" value="13"  class="checkBoxClass"/>
            </td>
        </tr>
        
         <tr style="text-align: center">
            <td>
               checkbox 4: <input type="checkbox" id="4" value="14"  class="checkBoxClass"/>
            </td>
        </tr>
        
    </table>
    <input type="hidden" id="hfCustomerID" />

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<script type="text/javascript">

    
    $(".checkBoxClass").click(function (e) {

        var allSelectedValues = "";
        var isFirst = true;

        $(".checkBoxClass:checked").each(function () {
            
            if (isFirst == true) {
                isFirst = false;
                
                allSelectedValues = $(this).val();
            } else {
                allSelectedValues = allSelectedValues + "," + $(this).val();
            }
           
        });

        $('#hfCustomerID').val(allSelectedValues);
        alert($('#hfCustomerID').val());

        
    });
</script>
 
Share this answer
 
Comments
Shridhar Salunkhe 28-Apr-17 3:14am    
and how to load hidden field value again in array?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900