2013-09-25 171 views
1

我使用jQuery的工具之前 - Validation Error Summary选择标签文本是文本框

/* adds an effect called "wall" to the validator */ 
     $.tools.validator.addEffect("wall", function(errors, event) { 
       // get the message wall 
       var wall = $(this.getConf().container).fadeIn(); 
       // remove all existing messages 
       wall.find("p").remove(); 
       // add new ones 
       $.each(errors, function(index, error) { 
        wall.append(
         "<p><strong>" +error.input.attr("name")+ "</strong> " +error.messages[0]+ "</p>" 
        ); 
       }); 
       // the effect does nothing when all inputs are valid 
     }, function(inputs) { 

     }); 

enter image description here

如果u可以看到图像一切正常,我要的是,更改此代码,而不是文本框名称,我希望标签文本在Error MSG之前显示。

在此先感谢。 编辑:HTML

<tbody style="" class="user_panel" id="customer_info"> 
    <tr> 
    <td valign="top"><label class="label">Contact name:</label> 
     <input type="text" value="" name="contact_name" class="" style="width:227px"></td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td valign="top"><label class="label">Measurement:</label> 
     <select value="" data-message="Measurement is required1" required="required" name="measurement" style="width: 231px; border-color: red;"> 
     <option value="">-select-</option> 
     <option value="1">mm</option> 
     <option value="2">inches</option> 
     </select></td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td valign="top"><label class="label">Phone personal:</label> 
     <input type="text" value="" data-message="Pers cont. num is required" required="required" name="contact_number_person" class="" style="width: 227px; border-color: red;"></td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
+0

你可以发布你的HTML吗? – Sadiq

+0

此处添加HTML! –

回答

1

更改你的JavaScript下//添加新的评论此:

$.each(errors, function(index, error) { 
    wall.append(
     "<p><strong>" +error.input.prev().html()+ "</strong> " +error.messages[0]+ "</p>" 
    ); 
}); 

而不是打印输入对象的名称属性,它会打印以前的元素是label

1

如果你将属性for添加到您的标签,例如

<label class="label" for="contact_name">Contact name:</label> 

然后,你可以有:

wall.append(
        "<p><strong>" +$("label[for='"+ error.input.attr("name")+"']").text()+ "</strong> " +error.messages[0]+ "</p>" 
       ); 

这解决了另一个元素是标签和输入之间的可能性。或者如果你想要一个标签来跟踪输入。