我有一个单选按钮,只能选中一个。但是错误信息显示在错误的地方,它显示在第一个单选按钮上,即“走进来”。我希望它在单选按钮的选项下显示,在“下方”的单选按钮下。
这是我的代码
<form class="form-horizontal" id="formApplication" role="form">
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnWalkIn" value="Walk-In"> Walk-In
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnJobFair" value="Job Fair"> Job Fair
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnSocialMedia" value="Social Media"> Social Media
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnReferred" value="Referred"> Referred
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnOther" value="Other"> Other
</div>
</div>
<button type="submit" class="btn pull-right" id="btnContinue1">Continue</button>
</form>
和JQuery
$("#formApplication").submit(function(e){
e.preventDefault();
}).validate({
rules:{
rbtnFind: "required"
},
messages: {
rbtnFind: "Please select an option"
},
highlight: function(element){
if($(element).attr('type') == 'radio'){
$(element.form).find("input[type=radio]").each(function(which){
$(element).closest('.form-group').addClass('has-error');
});
}
else {
$(element).closest('.form-group').addClass('has-error');
}
},
unhighlight: function(element){
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
submitHandler: function(form) {
//submit code
}
});
Here是JSFiddle中的代码。
答案 0 :(得分:1)
这里是我的小提琴 - https://jsfiddle.net/skyr9999/8nm3tvph/我希望它可以帮助你,我在js中做了更改并将占位符添加到html:
$("#formApplication").submit(function(e){
e.preventDefault();
}).validate({
rules:{
rbtnFind: "required"
},
messages: {
rbtnFind: "Please select an option"
},
highlight: function(element){
if($(element).attr('type') == 'radio'){
$(element.form).find("input[type=radio]").each(function(which){
$(element).closest('.form-group').addClass('has-error');
});
}
else {
$(element).closest('.form-group').addClass('has-error');
}
},
unhighlight: function(element){
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
$(".error_placeholder").html(error.html());
},
submitHandler: function(form) {
$(".error_placeholder").html("");
//submit code
}
});
答案 1 :(得分:1)
$("#formApplication").submit(function(e) {
e.preventDefault();
}).validate({
rules: {
rbtnFind: "required"
},
messages: {
rbtnFind: "Please select an option"
},
highlight: function(element) {
if ($(element).attr('type') == 'radio') {
$(element.form).find("input[type=radio]").each(function(which) {
$(element).closest('.form-group').addClass('has-error');
});
} else {
$(element).closest('.form-group').addClass('has-error');
}
//$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
console.log(error);
error.insertAfter('.has-error'); //Replace has-error with element
}
},
submitHandler: function(form) {
}
});
答案 2 :(得分:0)
我对您的脚本进行了一些更改,看看您是否希望这样做
<form class="form-horizontal" id="formApplication" role="form">
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnWalkIn" value="Walk-In"> Walk-In
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnJobFair" value="Job Fair"> Job Fair
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnSocialMedia" value="Social Media"> Social Media
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" name="rbtnFind" id="rbtnReferred" value="Referred"> Referred
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="radio" class="errorPlacement" name="rbtnFind" id="rbtnOther" value="Other"> Other
</div>
</div>
<div id="divFindFollowUp" class="form-group">
<!-- For the follow up for the question above -->
</div>
<button type="submit" class="btn pull-right" id="btnContinue1">Continue</button>
</form>
<script>
$("#formApplication").submit(function(e) {
e.preventDefault();
}).validate({
rules: {
rbtnFind: "required"
},
messages: {
rbtnFind: "Please select an option"
},
highlight: function(element) {
if ($(element).attr('type') == 'radio') {
$(element.form).find("input[type=radio]").last().closest('.form-group').addClass('has-error');
} else {
$(element).closest('.form-group').addClass('has-error');
}
//$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if ($(element).attr('type') == 'radio') {
error.insertAfter($(".errorPlacement").parent());
}
else{
if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
},
submitHandler: function(form) {
}
});
</script>