简体   繁体   English

jQuery形式serialize()不起作用

[英]jquery form serialize() doesn't work

Hi everyone I tried to serialize a form by his ID using jquery method serialize(). 大家好,我尝试使用jquery方法serialize()通过他的ID序列化表单。

Html code: HTML代码:

<form name="structure_form" id="structure_form" method="post">
    <div id="add_sections" class="postbox add_section_box">
        <button type="button" class="handlediv button-link" aria-expanded="true"><span class="screen-reader-text">Commuta il pannello: Add section</span><span class="toggle-indicator" aria-hidden="true"></span></button>
        <h3 class="handle ui-sortable-handle structure_title"><span>Add Section</span></h3>
        <div class="inside">
            #CONTENT#
            &nbsp;<input type="button" class="button button-primary button-large saveSections" id="save" value="Save">
        </div>
    </div>
</form>

Js code: js代码:

$('.saveSections').on('click', function () {
    var serializedForm = $('#structure_form').serialize();
    $.post(ajaxurl, {action: 'saveConfiguration', serializedForm: serializedForm}, function (data) {
        alert(data);
    });
    console.log("configuration saved");
});

Php code: 邮递区号:

function saveConfiguration() {
    print_r($_POST['serializedForm']);
    exit;
}



   function add_section_meta_box() {
    global $post;
    if ($post->post_type == "minisites") {
        $addSection = file_get_contents(dirname(__FILE__) . '/templates/components/add_section_box.html');
        $addSection = str_replace('#CONTENT#', Utils::createSelect(), $addSection);
    }
    echo Utils::includeScripts();
    echo $addSection;
}

This is Utils::createSelect 这是Utils :: createSelect

public static function createSelect() {
    $sections = get_posts(array(
        'post_type' => 'sections',
        'posts_per_page' => 10)
    );
    $html = '<select id="sections" name="item">';
    foreach ($sections as $section) {
        $html .= '<option value="' . $section->ID . '">' . $section->post_title . '</option>';
    }
    $html .= '</select><br><br>';
    $html .= '<input type="button" class="button button-primary button-large addSection" id="add" value="Add">';
    return $html;
}

the output is empty or undefined. 输出为空或未定义。 I think that the problem is the ID's form because if I change $('#structure_form') with $('form') it works fine. 我认为问题在于ID的格式,因为如果我将$('#structure_form')更改$('#structure_form') $('form')它就可以正常工作。

Thanks in advance. 提前致谢。

Edit(Update): 编辑(更新):

I add code in order that it works 我添加代码以使其正常工作

Create custom_post_type, box, ecc (Plugin) 创建custom_post_type,box,ecc(插件)

   <?php

//error_reporting(E_ALL);
//ini_set("display_errors", 'on');
/*
  Plugin Name: deaSiteBuilder
  Version: 1.0
  Description:
  Author: Giuseppe Giubaldo
  Author URI:
 */

require_once 'utils.php';
require_once __DIR__ . '/../../../vendor/autoload.php';

use Symfony\Component\Yaml\Parser;

add_action('init', 'create_post_type');

function create_post_type() {
    register_post_type('minisites', array(
        'labels' => array(
            'name' => __('Minisites'),
            'singular_name' => __('Minisite'),
        ),
        'public' => true,
        'has_archive' => true,
            )
    );
    register_post_type('sections', array(
        'labels' => array(
            'name' => __('Sections'),
            'singular_name' => __('Section'),
        ),
        'public' => true,
        'has_archive' => true,
    ));
}

//Add default meta box 
add_action('edit_form_after_editor', 'add_section_meta_box');

function add_section_meta_box() {
    global $post;
    if ($post->post_type == "minisites") {
        $addSection = file_get_contents(dirname(__FILE__) . '/templates/components/add_section_box.html');
        $addSection = str_replace('#CONTENT#', Utils::createSelect(), $addSection);
    }
    echo Utils::includeScripts();
    echo $addSection;
}

//Our custom meta box will be loaded on ajax 
function add_structure_meta_box($post_name) {
    $sectionStructureBox = file_get_contents(dirname(__FILE__) . '/templates/components/sectionStructureBox.html');
    $sectionStructureBox = str_replace('#ELEMENT#', strtoupper($post_name), $sectionStructureBox);
    $sectionStructureBox = str_replace('#CONTENT#', $post_name, $sectionStructureBox);
    echo $sectionStructureBox;
}

//Call ajax
add_action('wp_ajax_addStructureBox', 'addStructureBox');

function addStructureBox() {
    add_structure_meta_box($_POST['section']);
    exit;
}

add_action('wp_ajax_saveConfiguration', 'saveConfiguration');

function saveConfiguration() {
    echo $_POST['serializedForm'];
    exit;
}

Html pages: HTML页面:

sections_box sections_box

<form name="structure_form" id="structure_form" method="post">
    <div id="add_sections" class="postbox add_section_box">
        <button type="button" class="handlediv button-link" aria-expanded="true"><span class="screen-reader-text">Commuta il pannello: Add section</span><span class="toggle-indicator" aria-hidden="true"></span></button>
        <h3 class="handle ui-sortable-handle structure_title"><span>Add Section</span></h3>
        <div class="inside">
            #CONTENT#
            &nbsp;<input type="button" class="button button-primary button-large saveSections" id="save" value="Save">
        </div>
    </div>
</form>

structure_box structure_box

<div id="section_structure_box" class="postbox section_box">
<button type="button" class="handlediv button-link" aria-expanded="true"><span class="screen-reader-text">Commuta il pannello: Section Structure</span><span class="toggle-indicator" aria-hidden="true"></span></button>
<!--    <div class="handlediv" title="Click to toggle"><br>
    </div>-->
<h3 class="handle ui-sortable-handle structure_title"><span>Section Structure - #ELEMENT#</span></h3>
<div class="inside">
    #CONTENT#
</div>
<!--<input type="button" class="button button-primary button-large remove_box" id="remove" value="Remove">-->

Class Utils: 类实用程序:

    <?php

class Utils {

    public static function createSelect() {
        $sections = get_posts(array(
            'post_type' => 'sections',
            'posts_per_page' => 10)
        );
        $html = '<select id="sections" name="item">';
        foreach ($sections as $section) {
            $html .= '<option value="' . $section->ID . '">' . $section->post_title . '</option>';
        }
        $html .= '</select><br><br>';
        $html .= '<input type="button" class="button button-primary button-large addSection" id="add" value="Add">';
        return $html;
    }

    public static function includeScripts() {
        $html = '<link rel="stylesheet" type="text/css" href="' . plugins_url('deaSiteBuilder/assets/css/style.css', dirname(__FILE__)) . '">';
        $html .= '<script src="' . plugins_url('deaSiteBuilder/assets/js/jquery.min.js', dirname(__FILE__)) . '"></script>';
        $html .= '<script src="' . plugins_url('deaSiteBuilder/assets/js/jquery-ui.js', dirname(__FILE__)) . '"></script>';
        $html .= '<script src="' . plugins_url('deaSiteBuilder/assets/js/plugin.js', dirname(__FILE__)) . '"></script>';
        return $html;
    }

}

And js code: 和js代码:

    $(document).ready(function () {
    $('.addSection').on('click', function () {
        var selectedSection = $('#sections option:selected').text();
        $.post(ajaxurl, {action: 'addStructureBox', section: selectedSection}, function (data) {
            $('#add_sections').parent().append(data);
            console.log("section added");
        });
    });

    $('.saveSections').on('click', function () {
        var serializedForm = $('body form#structure_form').serialize();
        alert(serializedForm);
        $.post(ajaxurl, {action: 'saveConfiguration', serializedForm: serializedForm}, function (data) {

        });
        console.log("configuration saved");
    });
});

try this one, 试试这个

    $('.saveSections').on('click', function () {
        var serializedForm = $('#structure_form').serialize();

serializedForm.push({name: 'action', value: 'saveConfiguration'});
        $.post(ajaxurl, serializedForm, function (data) {
            alert(data);
        });
        console.log("configuration saved");
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM