jQuery-如果父级具有类,则删除prop

jQuery-如果父级具有类,则删除prop,jquery,validation,Jquery,Validation,我正在构建一个表单,并使用jquery验证来检查字段。 我有4个子类别,当你点击其中一个子类别时,它会获得类别“choice”,并且会出现一个输入文本来指定数量。我试图设置一个条件,如果该项具有“choice”类,它会在输入中添加“required”。但如果选择了其他项目,则无法将其删除。这是我的密码: jQuery('.offres_abo').click(function() { jQuery('.offres_abo').removeClass('choice'); jQuery(

我正在构建一个表单,并使用jquery验证来检查字段。 我有4个子类别,当你点击其中一个子类别时,它会获得类别“choice”,并且会出现一个输入文本来指定数量。我试图设置一个条件,如果该项具有“choice”类,它会在输入中添加“required”。但如果选择了其他项目,则无法将其删除。这是我的密码:

jQuery('.offres_abo').click(function() {
  jQuery('.offres_abo').removeClass('choice');
  jQuery(this).addClass('choice');
  if (jQuery('.offres_abo').hasClass('choice')) {
    jQuery(".offres_abo.choice input").prop('required', true);
  } else {
    jQuery(".offres_abo.choice input").prop('required', false);
  }
});


消防区长(8217);essai
3科利斯/安

<P>1(P<Juulle除外) 39欧元/年 Quantitéx
在删除类之前,选择需要修改的项

jQuery('.offres_abo').click(function(){
    jQuery('.offres_abo.choice')
        .removeClass('choice')
        .find('input')
        .prop('required', false);

    jQuery(this)
        .addClass('choice')
        .find('input')
        .prop('required', true);
});

选择删除类之前需要修改的项

jQuery('.offres_abo').click(function(){
    jQuery('.offres_abo.choice')
        .removeClass('choice')
        .find('input')
        .prop('required', false);

    jQuery(this)
        .addClass('choice')
        .find('input')
        .prop('required', true);
});

除了Yury Tarabanko answer,如果您想对多个项目使用
hasClass
,您可以使用函数

$('.offres_abo').each(function() {
  if($(this).hasClass('choice')) {
    // code
  }
});

除了Yury Tarabanko answer,如果您想对多个项目使用
hasClass
,您可以使用函数

$('.offres_abo').each(function() {
  if($(this).hasClass('choice')) {
    // code
  }
});

需要html结构我相信这
jQuery('.offres_abo').hasClass('choice')
返回的是集合,而不是单个元素。需要html结构我相信这
jQuery('.offres_abo').hasClass('choice')
返回的是集合,而不是单个元素。