检查页面中的每个 div 并根据 div 中的数据更改值
checking each div in page and change value according to the data in the div
我有通过显示套件创建的按钮(按钮包含 'join' 文本,我需要 运行 函数检查以查看值应该是加入还是离开。
你可以找到我的js代码和我的检查功能代码。我的问题是我认为我以错误的方式调用函数。
function init() {
$('div').each(function(){
$ajax('checkteam')
.then(function(){
if (res.data === true){
var button = $(this).find('.button.pulse');
$(button).text === 'Leave';
$(button).data('leave', true);
}
else {
}
}
)
})
}
'checkteam' => array(
'type' => MENU_CALLBACK,
'page callback' => 'card_generator_check_team_ajax',
'page arguments' => array(2),
'access arguments' => array('access content'),
),
function card_generator_check_team_ajax($term_name) {
global $user;
$term = reset(taxonomy_get_term_by_name($term_name));
$tid = $term->tid;
$user_fields = user_load($user->uid);
$arrlength = (sizeof(($user_fields->field_teams[und])));
for($x = 0; $x < $arrlength; $x++) {
$mytid = ($user_fields->field_teams[und])[$x]['tid'];
if ($tid == $mytid){
return true;
}
return false;
}
}
我需要从 checkteam 函数中获取数据,如果它是 true,那么应该保留该值。
您的 post 看起来像是 PHP 和 JavaScript 的疯狂组合。以下是我可能建议的一些基本更改。
$("div", context).each(function(i, el) {
var termName = $(".field-name-title .field-item", this).text().trim();
$.get(Drupal.settings.setProject.ajaxUrlcheck + "/" + termName,
function(res){
if (res === "false") {
$(".button.pulse", el).html("Leave");
}
}
);
});
.each()
遍历每个元素,所以可以得到索引和元素。您可以使用 $(el).find('.button.pulse')
或者您可以使用 $(".button.pulse", el)
来 select jQuery.
中的适当元素
这是我现在的代码,但它有一个问题,它总是执行一次,而不是每次都执行 (views-row)。我将在图片中附上我的布局。
(function ($) {
Drupal.behaviors.card_generator = {
attach: function (context, settings) {
$('.views-row', context).each(function(){
var button = $(this).find('.button.pulse'),
termName = $(this).find('.field-name-title .field-item');
$.ajax({
url: Drupal.settings.setProject.ajaxUrlcheck + '/' +
$(termName).text(),
method: "GET",
})
.then(function(res){
if (res === 'true'){
$(button).text('Leave');
}
}
)
})
}
};
})(jQuery);
问题解决了! php 函数错误。
function card_generator_check_team_ajax($term_name)
{
global $user;
$term = reset(taxonomy_get_term_by_name($term_name));
$tid = $term->tid;
$user_fields = user_load($user->uid);
$arrlength = (sizeof(($user_fields->field_teams[und])));
for ($x = 0; $x < $arrlength; $x++) {
$mytid = ($user_fields->field_teams[und])[$x]['tid'];
if ($tid == $mytid) {
$arr = 'true';
continue;
}
}
drupal_json_output($arr);
}
我有通过显示套件创建的按钮(按钮包含 'join' 文本,我需要 运行 函数检查以查看值应该是加入还是离开。 你可以找到我的js代码和我的检查功能代码。我的问题是我认为我以错误的方式调用函数。
function init() {
$('div').each(function(){
$ajax('checkteam')
.then(function(){
if (res.data === true){
var button = $(this).find('.button.pulse');
$(button).text === 'Leave';
$(button).data('leave', true);
}
else {
}
}
)
})
}
'checkteam' => array(
'type' => MENU_CALLBACK,
'page callback' => 'card_generator_check_team_ajax',
'page arguments' => array(2),
'access arguments' => array('access content'),
),
function card_generator_check_team_ajax($term_name) {
global $user;
$term = reset(taxonomy_get_term_by_name($term_name));
$tid = $term->tid;
$user_fields = user_load($user->uid);
$arrlength = (sizeof(($user_fields->field_teams[und])));
for($x = 0; $x < $arrlength; $x++) {
$mytid = ($user_fields->field_teams[und])[$x]['tid'];
if ($tid == $mytid){
return true;
}
return false;
}
}
我需要从 checkteam 函数中获取数据,如果它是 true,那么应该保留该值。
您的 post 看起来像是 PHP 和 JavaScript 的疯狂组合。以下是我可能建议的一些基本更改。
$("div", context).each(function(i, el) {
var termName = $(".field-name-title .field-item", this).text().trim();
$.get(Drupal.settings.setProject.ajaxUrlcheck + "/" + termName,
function(res){
if (res === "false") {
$(".button.pulse", el).html("Leave");
}
}
);
});
.each()
遍历每个元素,所以可以得到索引和元素。您可以使用 $(el).find('.button.pulse')
或者您可以使用 $(".button.pulse", el)
来 select jQuery.
这是我现在的代码,但它有一个问题,它总是执行一次,而不是每次都执行 (views-row)。我将在图片中附上我的布局。
(function ($) {
Drupal.behaviors.card_generator = {
attach: function (context, settings) {
$('.views-row', context).each(function(){
var button = $(this).find('.button.pulse'),
termName = $(this).find('.field-name-title .field-item');
$.ajax({
url: Drupal.settings.setProject.ajaxUrlcheck + '/' +
$(termName).text(),
method: "GET",
})
.then(function(res){
if (res === 'true'){
$(button).text('Leave');
}
}
)
})
}
};
})(jQuery);
问题解决了! php 函数错误。
function card_generator_check_team_ajax($term_name)
{
global $user;
$term = reset(taxonomy_get_term_by_name($term_name));
$tid = $term->tid;
$user_fields = user_load($user->uid);
$arrlength = (sizeof(($user_fields->field_teams[und])));
for ($x = 0; $x < $arrlength; $x++) {
$mytid = ($user_fields->field_teams[und])[$x]['tid'];
if ($tid == $mytid) {
$arr = 'true';
continue;
}
}
drupal_json_output($arr);
}