2015-04-17 122 views
1

我有一个表格有几个输入,并在他们旁边(左边)有一个相应的标签。jquery:立即获得之前的兄弟或元素的父亲

当我验证输入时,我想显示一个消息,使用相应的标签来告诉哪个输入错误。

我有一个显示为我想要的唱片公司工作的脚本,但它打破了,如果我的输入是不是标签的直接兄弟,但兄弟姐妹的孩子,而不是...

这里是为了说明的代码:

HTML:

<form> 
<label class="autoFormLabel obligatorio">* calle : </label> 
<input id="calle2" class="autoForm obligatorio" type="text" value="" name="calle2"> 
<br> 
<label class="autoFormLabel obligatorio">* CP : </label> 
<input id="CP" class="autoForm obligatorio" type="text" value="" name="CP"> 
<br> 
<label id="label_colonia" class="autoFormLabel obligatorio">* Colonia : </label> 
<span id="span_combo_colonias"> 
    <select id="Colonia" class="obligatorio autoForm" name="Colonia"> 
    <option selected="" value="0">-- seleccione --</option> 
     <option value="1">one</option> 
     <option value="2">two</option> 
    </select> 
</span> 
<br> 
<label class="autoFormLabel obligatorio">* Delegacion : </label> 
<input id="delegacion" class="autoForm obligatorio" type="text" value="" name="delegacion" > 
<br> 

    <input type="button" id="val" value="Val" /> 

</form> 

JS:

function validate(){ 
    $(":input.obligatorio:enabled").each(function(i, el){ // solo considerar los enableds    
     if($(this).val() == "" || $(this).val() == "0" || $(this).val() == 0){ 

      label = $(this).prev("label").html(); //A: works for all but select    
      //label = $(this).parent().prev("label").html(); //B: works for select, but not the others   
      //label = $(this).closest("label").html(); //C: why doesn't work for select? 

      alert("El campo:\n\n"+label+"\n\nno puede ir vacio"); 
      $(this).focus(); 
      return false; 
     }  
    }); 
} 

和这里的a fiddle to play with

所有的输入都有一个对应的兄弟标签,除了<select>,它在<span>之内,它本身就是其标签的兄弟。

我选择label = $(this).prev("label").html();作品完美excpet的选择所有的输入,因为它包含一个跨度内(我需要它这样的,因为这样的选择是动态的,并得到通过AJAX插入这样的跨度内)

我知道我可以使它与选择案例的另一个选择器一起工作,如下所示:label = $(this).parent().prev("label").html(); 但理想情况下,我希望只有一个选择器可以获取所有标签,而不管其相应输入的深度如何。我也试过用.closest()选择器,但它不起作用,我不知道为什么,但我认为它没有,因为标签不是select的上升(它是一个它的父母的兄弟,[让我们说一个'叔叔]])

那么哪个选择器可以用于所有情况下,尽管是父母,兄弟姐妹,父母兄弟或即使是盛大的盛大父母,无论...(我可以有一个输入埋在其标签旁边的几个跨度内,或其他任何东西)

换句话说,我如何遍历DOM“上方和左边”并获得选择者的第一个选择,无论他们是父母还是他们 兄弟姐妹?

+0

@nirmal,小提琴已经存在。 –

+0

@Nimal问题完整清晰 – mplungjan

+1

https://gist.github.com/ryankirkman/6630488 – mplungjan

回答

2

使用for在您的标签属性:

label = $('[for="'+this.id+'"]').html(); 

JSFiddle


喷气另一种方法是将存储:

<label for="calle2" class="autoFormLabel obligatorio">* calle : </label> 
<input id="calle2" class="autoForm obligatorio" type="text" value="" name="calle2"/> 
... 

然后使用以下jQuery选择抢标签文本“消息“标签中的输入的data属性/选择而不是搜索的DOM中的元素:

<input id="calle2" data-label="Calle" class="autoForm obligatorio" type="text" value="" name="calle2"/> 

的jQuery:

label = $(this).attr('data-label'); 

JSFiddle

+0

将该for添加到标签后。好主意 – mplungjan

+0

哇!我不知道标签元素中的“for”属性。在我的情况下,它肯定会工作,但是,我仍然想知道如何用jQuery来实现这样的选择。如果我想选择其他父/兄弟元素而不是标签,该怎么办?谢谢! – DiegoDD

+0

...此外,如果我有几个连续输入的标签,'for'选项将不起作用。但是,我可以在标签上使用一个虚拟属性,并添加所有相应的输入的id,并使用稍微不同的选择器,如'$('[dummy〜=''+ this.id +'“]')。html );'。是啊! – DiegoDD