我只在选择另一个组合框时尝试启用select2字段。这对于文本框、普通输入甚至简单的select元素都很好,但是当我尝试在使用Ajax调用的select2中使用ng禁用时,它就不能工作了。
下面是我代码的一部分:
<section class="col col-sm-4">
<label class="label" data-localize="Tipo de Participante"></label>
<select id="select_tipo_participante" name="select_tipo_participante" ng-model="filtros.F_ID_TIPO_PARTICIPANTE.Valor"
class="select2" data-placeholder="Selecione..."
ng-disabled="filtros.F_ID_USUARIO.Valor == null">
<option value="null" data-localize="Selecione uma opção"></option>
<option ng-repeat="tipo in dados_listas.tipos_participante" value="{{tipo.ID_TIPO_PARTICIPANTE}}">
{{tipo.NM_TIPO_PARTICIPANTE}}
</option>
</select>
</section>
<section class="col col-sm-4">
<label class="label" data-localize="Novo Usuário"></label>
<input name="select_novo_usuario" id="select_novo_usuario" ng-model="filtros.F_ID_USUARIO.Valor" ng-disabled="filtros.F_ID_TIPO_PARTICIPANTE.Valor == null"/>
</section>
<section class="col col-sm-12">
<label class="label" data-localize="Justificativa"></label>
<textarea rows="3" class="form-control" name="justificativa_redesignacao" ng-model="DS_JUSTIFICATIVA" ng-disabled="filtros.F_ID_TIPO_PARTICIPANTE.Valor == null"></textarea>
我希望将第二个元素(id="select_novo_usuario")禁用,直到选择上一个元素为止。
使用ng禁用的textarea运行得很好。
这里是我的ajax调用:
$("#select_novo_usuario").select2({
minimumInputLength: 4,
allowClear: true,
placeholder: localize.localizeText('tooltip_select_interessado'),
ajax: {
url: BACKEND_URL + 'usuario_interessado',
dataType: 'json',
type: "GET",
quietMillis: 50,
data: function (term) {
var filter = 'NM_USUARIO,like,' + term;
return {
filters: filter,
filter_id_interessado: 'ID_USUARIO,notin,teste.teste'
};
},
results: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.NM_USUARIO,
slug: item.NM_USUARIO,
id: item.ID_USUARIO
}
})
};
},
},
});
$("#select_novo_usuario").on("select2-selecting", function(e) {
$timeout(function(){
$scope.$evalAsync(function() {
$scope.filtros.F_ID_USUARIO.Valor = e.val;
});
});
});
我怎么才能让它起作用?
发布于 2015-05-04 09:34:35
我无法使用禁用ng的方式使其工作,所以我在我的.js文件中这样做,使我想要禁用的选择响应上一次$watch。下面是我作为sampe的代码:
$scope.$watch('filtros.F_ID_TIPO_PARTICIPANTE.Valor', function(newVal, oldVal, scope) {
if(newVal != null) {
$('#select_novo_usuario').select2("enable", true);
$('#select_novo_usuario').select2("val", "");
}
$('#select_novo_usuario').select2("enable", false);
$('#select_novo_usuario').select2("val", "");});
发布于 2015-04-30 11:20:33
看来您已经正确设置了它。
尝试删除“选择”上的“ng-disabled="filtros.F_ID_USUARIO.Valor == null
”,它们将禁用彼此之间的,并显示为。
如果我正确地读取了您的代码,则在选定某些内容之前不会填充输入,对吗?由于它们似乎是相互禁用的,所以似乎两者都不应该启用。
如果未触及select,则应将其设置为null。输入和文本区域上禁用ng的值都应该返回null。这里有一个柱塞,它证明了:
输入和文本区域都将被禁用,直到您在选择中选择一个项为止。
发布于 2019-12-19 00:45:24
ng-残疾人似乎与Select2不合作。
试试这个:
//As of Select2 4.1, they've removed support for .enable
$("select").prop("disabled", true);
// instead of $("select").enable(false);
https://stackoverflow.com/questions/29973936
复制