la classe delle jquery per i bambini di tipo particolare
-
30-09-2019 - |
Domanda
Ho didascalia etichetta associata con più controlli, che dovrebbe diventare rosso quando uno di esso è sbagliato.
<tr>
<th>
<asp:Label for="test">jquery will make my class as updnValidationErrorLabel</asp:Label>
</th>
<td > this text is right
<asp:TextBox class='textboxWide taskListCheckPreVital' />
</td>
<td>this is wrong text hence it has updnValidationErrorInput
<asp:TextBox class='dummyclass updnValidationErrorInput'/></td>
</tr>
sto provando questo approccio, ma non elemento di sicuro perché mainparent bambini non mostrare alla classe updnValidationErrorInput
//if my sturcture has updnValidationErrorInput
$('.updnValidationErrorInput').each(function() {
// go to tr element
var mainParent = $(this).parents('tr:first');
// under tr element find updnValidationErrorInput
if(mainParent.children('.updnValidationErrorInput').length > 0){
// set label which has for attribute with updnValidationErrorLabel
mainParent.children('label').attr('for').removeClass().addClass('updnValidationErrorLabel');
}
});
Ogni aiuto sarà molto apprezzato.
Soluzione
Dato che si sta venendo da il <input>
controlli .length
deve essere vero, in modo da poter rimuoverli. È possibile ridurre il basso a questo complesso:
$('.updnValidationErrorInput').each(function() {
$(this).cloesest('tr').find('label[for]')
.removeClass().addClass('updnValidationErrorLabel');
});
Invece di .parents('tr:first')
è possibile utilizzare l'equivalente (e meno costoso) .closest()
, quindi trovare elementi <label>
con l'attributo for=""
e cambiare la classe su di loro
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow