如何使用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);
}
}