JS POST更新数据分类

时间:2020-10-01 作者:Anarko

如何使用js函数调用每个术语的值?我需要使用数组

`  useractivities=[];

          $(\'.user_activities\').each(function(){
          useractivities.push( $(this).val() ) 
        }); `
?怎么做

这是我的代码:

HTML视图:

<div class="multiselect">

<div id="user_activities_form">
       <label for="user_activities-randonnee">
        <input type="checkbox" name="user_activities[]" id="user_activities-randonnee" value="10028">
        RANDONNEE          </label><br>
       <label for="user_activities-vtt">
        <input type="checkbox" name="user_activities[]" id="user_activities-vtt" value="10029">
        VTT          </label><br>
          
</div>
JS调用

  useractivities=[];
      $(\'.user_activities\').each(function(){
      useractivities.push( $(this).val() ) 
    });  // <==HERE 
         $.ajax({
            type: \'POST\',
            url: ajaxurl,
            data: {
                \'action\'            :   \'wpestate_ajax_update_profile\',
                \'user_activities\'         :   useractivities   
            },
            success: function (data) {

                $(\'#profile_message\').empty().append(\'<div class="login-alert">\' + data + \'<div>\');
            },
            error: function (errorThrown) {
            }
        });
更新功能(wpestate_ajax_update_profile):

             $current_user   =   wp_get_current_user();
             $userID         =   $current_user->ID;
             $useractivities = get_terms($_POST[\'user_activities\'], array( \'hide_empty\' => false ) );

             
 foreach($useractivities as $useractivitie){
    $useractivitieID = $useractivitie->term_id;
    if (\'POST\' === $_SERVER[\'REQUEST_METHOD\']) {
                if(!isset($_POST[\'user_activities\']))
                wp_set_object_terms($userID,$useractivitieID,\'user_activities\',false); 
                else 
                wp_set_object_terms($userID,$useractivitieID,\'user_activities\',true); 

    }   
 }

1 个回复
SO网友:Bitcoin Solution

在这种情况下,您需要查找;“名称”;或“或”;“值”;。

if( isset( $_POST[\'user_activities-\'.$useractivitie->slug] ) )
更改为:

if( isset( $_POST[\'user_activities\'][$useractivitie] ) )

相关推荐

Filter Custom Taxonomy Posts

我有一个自定义的分类页面,它也有一个向上的滑块。问题是滑块显示来自所有分类法而非当前分类法的随机帖子。是否有办法过滤滑块中的图像,使其仅使用当前分类法?<?php global $taxonomy_location_url, $taxonomy_profile_url; $args = array( \'post_type\' => $taxonomy_profile_url, \'orderby\' => \'rand\', \'meta_que