2017-04-22 54 views
3

在我的表單中,我有很多input type =「radio」字段,它們是由PHP循環代碼生成的。每個都有自己的名稱,這是一個數組。這些輸入字段的值通過AJAX進行處理。問題是我不知道如何在JavaScript或jQuery中讀取這些輸入字段的值。獲取多個PHP變量到一個Javascript數組中

這是我的表單代碼(簡體)。

<form id="_user_roles_form" method="post" class="form-horizontal"> 

    <p><strong>Manager</strong></p> 

    <div class="form-group row"> 
     <label class="col-lg-6 control-label">Register Companies</label> 
     <div class="col-lg-6"> 
      <div class="btn-group" data-toggle="buttons"> 
       <label class="btn btn-default active"> 
        <input type="radio" name="_data[Manager][publish_companies]" value="1"/> True 
       </label> 
       <label class="btn btn-default "> 
        <input type="radio" name="_data[Manager][publish_companies]" value="0"/> False 
       </label> 
      </div> 
     </div> 
    </div> 

    <div class="form-group row"> 
     <label class="col-lg-6 control-label">Edit Companies</label> 
     <div class="col-lg-6"> 
      <div class="btn-group" data-toggle="buttons"> 
       <label class="btn btn-default active"> 
        <input type="radio" name="_data[Manager][edit_companies]" value="1"/> True 
       </label> 
       <label class="btn btn-default "> 
        <input type="radio" name="_data[Manager][edit_companies]" value="0"/> False 
       </label> 
      </div> 
     </div> 
    </div> 

    <div class="form-group row"> 
     <label class="col-lg-6 control-label">Edit Others Companies</label> 
     <div class="col-lg-6"> 
      <div class="btn-group" data-toggle="buttons"> 
       <label class="btn btn-default active"> 
        <input type="radio" name="_data[Manager][edit_others_companies]" value="1"/> True 
       </label> 
       <label class="btn btn-default "> 
        <input type="radio" name="_data[Manager][edit_others_companies]" value="0"/> False 
       </label> 
      </div> 
     </div> 
    </div> 

    <div class="form-group row"> 
     <label class="col-lg-6 control-label">Edit Customers</label> 
     <div class="col-lg-6"> 
      <div class="btn-group" data-toggle="buttons"> 
       <label class="btn btn-default "> 
        <input type="radio" name="_data[Employee][edit_customers]" value="1"/> True 
       </label> 
       <label class="btn btn-default active"> 
        <input type="radio" name="_data[Employee][edit_customers]" value="0"/> False 
       </label> 
      </div> 
     </div> 
    </div> 

    <div class="form-group row"> 
     <label class="col-lg-6 control-label">Edit Others Customers</label> 
     <div class="col-lg-6"> 
      <div class="btn-group" data-toggle="buttons"> 
       <label class="btn btn-default "> 
        <input type="radio" name="_data[Employee][edit_others_customers]" value="1"/> True 
       </label> 
       <label class="btn btn-default active"> 
        <input type="radio" name="_data[Employee][edit_others_customers]" value="0"/> False 
       </label> 
      </div> 
     </div> 
    </div> 

    <div class="form-group row"> 
     <label class="col-lg-6 control-label">Delete Customers</label> 
     <div class="col-lg-6"> 
      <div class="btn-group" data-toggle="buttons"> 
       <label class="btn btn-default "> 
        <input type="radio" name="_data[Employee][delete_customers]" value="1"/> True 
       </label> 
       <label class="btn btn-default active"> 
        <input type="radio" name="_data[Employee][delete_customers]" value="0"/> False 
       </label> 
      </div> 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-lg-12"> 
      <button type="submit" class="btn btn-default">Confirm</button> 
     </div> 
    </div> 

</form> 

在PHP中,我可以使用_data [] []作爲數組變量,但我需要讀入一個JavaScript變量,這樣我可以將它傳遞到AJAX。通常我會通過jQuery('#_ ID')。val(),但在這種情況下它不起作用。我覺得我需要聲明一個javascript數組變量,找到所有(和每個)HTML元素,並將每個找到的元素的名稱和值都推送到數組中陣列。我不知道我該如何解決這個問題。

非常感謝您的幫助。謝謝。

+0

所以要通過AJAX發送這種形式的所有投入? – David

+0

@David是的,我想通過Ajax發送這種形式的所有輸入。 – Dongsan

回答

1

如果您要發送所有值,請使用serializeArray()函數。

var formData = jQuery('#_user_roles_form').serializeArray(); 

如果你使用WordPress,你可能還需要:

formData.push({name: 'action', value: 'yourAjaxAction'}); 
// if you have set yourself up to use nonce 
// formData.push({name: 'nonce_data', value: yourNonceValue }); 

jQuery.ajax({ 
    url   : ajaxUrl, // global ajax url 
    type  : 'post', 
    data  : formData, 
    success  : function(data, textStatus, jqXHR) { 
// etc etc 
+0

工程就像一個魅力!向serializeArray()和Melissa Freeman致敬! – Dongsan

+1

太棒了!很高興你解決了它。 –

相關問題