1

onclickの機能: -javaScript配列を文字列に変換する方法とMVCの隠しフィールドに保存する方法は?

//$(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. 私はページングをクリック
  4. は、隠されたフィールドの値が配列にロードする必要があり、その後、配列に、それはまた、隠しフィールド

答えて

2

サンプル

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

から既存の値を持つ新しい点検値を格納しますHTMLページ。

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

jqueryを使用すると値を設定できます。

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

よりクリーンなアプローチを使用することである[ '.join()'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) 'toString()'はObjectの一部であり、javascriptのすべてのオブジェクトで使用できるためです。アレイのみを受信する場合は、前者を使用します。 – KarelG

関連する問題