需要一些帮助来解决这个错误,所以基本上我在我的HTML中就有这个错误
<div class="form-group">
<div class="col-xs-3">
<label class="control-label">Category</label>
</div>
<div class="col-xs-9">
<input id="prog_categ" name="category" class="form-control" required='required' placeholder="-Select Category-">
</div>
</div>
我的FormValidation的JS代码基本上遵循http://formvalidation.io/examples/select2/:
$('#newProgram')
// IMPORTANT: You must declare .on('init.field.fv')
// before calling .formValidation(options)
.on('init.field.fv', function(e, data) {
var $parent = data.element.parents('.form-group'),
$icon = $parent.find('.form-control-feedback[data-fv-icon-for="' + data.field + '"]');
$icon.on('click.clearing', function() {
// Check if the field is valid or not via the icon class
if ($icon.hasClass('glyphicon-remove')) {
// Clear the field
data.fv.resetField(data.element);
}
});
})
//Revalidate the category when it is changed
.find('[name="category"]')
.select2()
.change(function(e) {
$('#newProgram').formValidation('revalidateField', 'category');
}).end()
.formValidation({
framework : 'bootstrap',
excluded : [':disabled'],
addOns : {
i18n : {},
// mandatoryIcon: {
// icon:'fa fa-asterisk'
// },
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
}
},
fields: {
category: {
validators: {
callback: {
message: 'Please choose 2-4 color you like most',
callback: function(value, validator, $field) {
// Get the selected options
var options = validator.getFieldElements('category').val();
return (options != null && options.length >= 2 && options.length <= 4);
}
}
}
},
}
})
.on('success.form.fv', function(e) {
var $form = $(e.target), // Form instance
//Get the clicked button
$button = $form.data('formValidation').getSubmitButton(),
//You might need to update the "status" field before submitting the form
$statusField = $form.find('[name="publishStatus"]');
//To demonstrate which button is clicked,
//You might don't need to use it in real application
switch ($button.attr('id')) {
case 'publishButton':
$statusField.val('publish');
alert('The article will be published');
break;
case 'draftButton':
$statusField.val('draft');
alert('The article will be saved as a draft');
break;
case 'editButton':
$statusField.val('edit');
alert('The article will be saved as a draft');
break;
default:
$statusField.val('unpublished');
alert('The article will be saved');
break;
}
});
我的Select2数据在此表单中是本地的
[{"id":"1","text":"categ1"},{"id":"2","text":"categ2"},{"id":"3","text":"categ3"},....].
现在页面加载正确,我的select2字段工作正常,但验证将不起作用。我得到一个错误"未捕获的异常:未为Select2 prog_categ定义查询函数"停止所有其他要验证的字段。
有人能指出我在这件事上哪里错了吗?
感谢
问题与两个插件之间的不兼容有关。在我使用的select2版本中,select2使用HTML 中的输入div构建了Select选项
然而,FormValidation.io从一开始就在DOM上寻找一个"select"标记,我在那里有"input"标记。我更新到了select2的最新版本,他们实际上建议不要使用"输入",而是使用"选择"。因此,使用Select2的最新版本将我的代码更改为,一切正常,没有任何错误。希望这能让人不那么头疼。
<div class="form-group">
<label class="control-label"></label>
<select id="categ" name="category" class="form-control select2" required='required' style="width: 100%"'>
<option></option>
</select>
</div>