2017-08-28 188 views
3

我试图通过编程方式添加自定义元框,但js给出了一个错误,目标是当我点击添加按钮时,同一个文本区框添加了哪个 是以前创建。我对在二17主题装箱定制metabox在function.php文件中的代码是:如何在post中添加自定义元字段与jQuery

// including custom js files here 
function wptuts_scripts_with_jquery() { 
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/myjs.js', array('jquery')); 
} 
add_action('admin_enqueue_scripts', 'wptuts_scripts_with_jquery'); 

// starting custom meta box 
add_action('add_meta_boxes', 'm_param_meta_box_add'); 
function m_param_meta_box_add() { 
    add_meta_box('m_param_post', 'Box Title', 'm_param_post_meta_box_cb', 'post', 'normal', 'high'); 
} 

function m_param_post_meta_box_cb($post) 
{ 
    $values = get_post_custom($post->ID); 
    if (isset($values['m_meta_description'])) { 
     $m_meta_description_text = esc_attr($values['m_meta_description'][0]); 
    } 
    wp_nonce_field('my_meta_box_nonce', 'meta_box_nonce'); 

?> 

<div id="info"></div> 
    <label> Name: </label> 
    <textarea rows="1" cols="20" name="m_meta_description" ><?php echo $m_meta_description_text; ?></textarea> 

    <button type="button" name="add" id="add">Add</button><br><br> 

<?php 
} // close m_param_post_meta_box_cb function 

add_action('save_post', 'cd_meta_box_save'); 
function cd_meta_box_save($post_id) 
{ 
    // Bail if we're doing an auto save 
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

    // if our nonce isn't there, or we can't verify it, bail 
    if(!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'my_meta_box_nonce')) return; 

    // if our current user can't edit this post, bail 
    if(!current_user_can('edit_post')) return; 

    // Make sure your data is set before trying to save it 
    if(isset($_POST['m_meta_description'])) { 
     update_post_meta($post_id, 'm_meta_description', wp_kses($_POST['m_meta_description'])); 
    }  
} 

我的js代码的错误是:“遗漏的类型错误:$不在myjs.js功能 :2

我的js代码是:在使用本

// <script type="text/javascript"> 
     $(document).ready(
      function(){ 
       $("#add").click(function(){ 
        $("#info").append('fname<input type="text" name="name[]">'); 
       }) 
      } 
     ); 
    // </script> 
+0

护理的需要分享什么样的JS错误的你重新获得? –

+0

编辑我的问题。 – Abhee

回答

1

尝试:

jQuery(document).ready(
     function(){ 
      jQuery("#add").click(function(){ 
       jQuery("#info").append('fname<input type="text" name="name[]">'); 
      }) 
     } 
    ); 

this文章的时候WordPress的jQuery是加载,它采用兼容模式,这是避免与其他语言库冲突的机制:

What this boils down to is that you can’t use the dollar sign directly as you would in other projects. When writing jQuery for WordPress you need to use jQuery instead.

+0

尝试你的代码,完美的作品,现在我有一个问题,当我点击添加按钮时,文本区域被成功追加,但它不能为其他元框保存值,只有第一个元框值正在保存,你有没有对这个问题的建议? – Abhee

+0

我不知道为什么它不能保存。抱歉。我建议就这个新问题提出另一个问题。 – user8230352

+0

选中此链接:https://wordpress.stackexchange.com/questions/23344/why-wont-my-metabox-data-save。 – user8230352

1

虽然this answer是正确的,这是值得一说的,你可以也做到这一点

jQuery(document).ready(function ($) 
{ 
    $("#add").click(function(){ 
     $("#info").append('fname<input type="text" name="name[]">'); 
    }) 
}) 

去除写jQuery随处可见,而不是$

相关问题