将多个 PHP 变量放入 JavaScript 数组中

3
在我的表单中,有许多由PHP循环代码生成的字段。每个都有自己的数组名称。这些输入字段的值通过AJAX进行处理。问题是,我不确定如何在Javascript或jQuery中读取这些输入字段的值。
以下是简化后的表单代码。
<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元素(包括每个)type="radio" & name="_data-*",并将找到的每个元素的名称和值推入数组中。我不确定如何解决这个问题。
非常感谢您的帮助。谢谢。

你想通过Ajax发送这个表单中的所有输入吗? - David
@David 是的,我想通过Ajax发送此表单中的所有输入。 - Dongsan
1个回答

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

运行得非常好!向serializeArray()和Melissa Freeman致敬! - Dongsan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接