检查页面中的每个div,并根据div中的数据更改值

时间:2019-07-12 17:14:32

标签: javascript php jquery drupal drupal-7

我有通过显示套件创建的按钮(这些按钮包含“ join”文本,我需要运行功能检查以查看该值应为join还是left。 您可以找到我的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,则应保留该值。

3 个答案:

答案 0 :(得分:0)

您的帖子看起来像是PHP和JavaScript的疯狂结合。这是我可能建议的一些基本更改。

  const Component = () => (
  <div className={`${styles["body"]}`}>
    {heroesArray.map((hero, i) => {
      let randomQuote = getRandomQuote(hero.quotes);
      let path = hero.name
        .toLowerCase()
        .split(" ")
        .join("_");
      return (
        <div className="thumbnail">
          <Thumbnail
            key={i}
            hero={hero}
            randomQuote={randomQuote}
            path={path}
          />
          <Route <!-- HERE I CREATE A ROUTE -->
            path={path}
            component={() => <HeroPage hero={hero} />}
            key={i + 365}
          />
        </div>
      );
    })}
  </div>
);

df1['Votes'] = '' df1['Votes']=df1['movie'].apply(lambda title: df2[df2['FILM'].str.startswith(title)]['Votes'].any(0)) 迭代每个元素,因此您可以获得索引和元素。您可以使用 $("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()在jQuery中选择适当的元素。

如果需要进一步的帮助,请发布a Minimal, Reproducible Example

希望这会有所帮助。

答案 1 :(得分:0)

这是我的代码,但是有一个问题总是总是执行一次,而不是每次执行一次(视图行)。我将在图片中附加我的布局。 this is my layout

(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);

答案 2 :(得分:0)

问题解决了! 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);
}
相关问题