2016-06-20 1 views
0

Я пытаюсь добавить проверку на стороне сервера, которая прекрасна, но я хочу отображать сообщение под каждым отдельным входом. Это пример моего возвращения JSon:Добавление сообщения проверки формы под вход через результат json

{ 
    "title":"The title field is required", 
    "description":"The description field is required", 
    "price":"The price field is required", 
    "startingprice":"The startingprice field is required", 
    "startprice":"The startprice field is required", 
    "type":"The type field needs to contain one of these values: auction", 
    "condition":"The condition field needs to contain one of these values: new", 
    "auctionlength":"The auctionlength field is required", 
    "shortdescription":"The shortdescription field is required", 
    "offers":"The offers field is required", 
    "returns":"The returns field needs to contain one of these values: accept", 
    "country":"The country field is required", 
    "shippingcost":"The shippingcost field is required" 
} 

Это, как я пытаюсь сделать это с помощью JQuery:

$.each(data, function(input, message) 
{ 
    console.log(input); 
    $('input[name='+input+']').append(message); 
}); 

и образец моей формы:

<input type="text" name="title"> 
<input type="text" name="description"> 

I проверили мой журнал консоли, и не получаю никаких ошибок, никакой помощи?

ответ

1

Вы хотите использовать .after() вместо append.

$.each(data, function(name, message) { 
    $('input[name='+ name +']').after('<div class="error">' + message + '</div>'); 
}); 

Demo

Смежные вопросы