正如你在我的代码中看到的,我一直使用errorPlacement方法。 我首先尝试在方法内部使用if else语句,并且错误消息至少会显示,但仅在输入框下方显示,而不是像我想要的那样并排显示。 由于我有6个输入框要测试,因此我使用switch语句,但这对我根本行不通。 我已将一些CSS应用于包含输入框的div,我不知道这是否会干扰消息的位置或其他方面。
HTML
<div id ="column1">
<label>Name of Show</label><input type="text" name="performance" id="performance" />
<label>Name of location</label> <input type="text" name="location" id="location"/>
<label>Date</label> <input type="text" id="date" name="date"/>
</div>
<div id="column2">
<label>Time</label><input type="text" id="time" name="time"/>
<label># of Volunteers Needed</label><input type="text" id="guests" name="guests"/>
<label>Cover</label><input type="text" id="price" name="price"/>
</div>
JS
//this is just the error placement block, the other validate() code is working fine and omitted
errorPlacement: function(error, element){
switch(element)
{
case element.attr("name") === 'performance':
error.insertAfter( $("#performance") );
break;
case element.attr("name") === 'location':
error.insertAfter( $("#location") );
break;
case element.attr("name") === 'date':
error.insertAfter( $("#date") );
break;
case element.attr("name") === 'time':
error.insertAfter( $("#time") );
break;
case element.attr("name") === 'guests':
error.insertAfter( $("#guests") );
break;
case element.attr("name") === 'price':
error.insertAfter( $("#price") );
break;
default:
//nothing
}
},