我有以下大量代码:
$(function() {
$('#dataSource').select2({ multiple: true, data: @Html.Raw(Model.AvailableDataSources) });
$("#dataSource").bind("change", function() {
if ($("#dataSource").val().length <= 0) {
//Disable the select2()
}
});
});
如果没有数据源,我需要禁用 .select2 字段。我怎样才能禁用它?
最佳答案
完整的工作示例:
只禁用一个字段:
$("#dataSource").change(function(){
if (!$("#dataSource").val())
{
$(this)[0].disabled = true ;
}
})
function enableDataSource()
{ $("#dataSource")[0].disabled = false ;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="dataSource" value="" />
<button onclick="enableDataSource()">Enable field</button>
通过类名禁用多个字段:
$(".dataSource").change(function(){
if (!$(this).val())
{
$(this).attr('disabled', true) ;
}
})
function enableDataSource()
{ $(".dataSource").attr('disabled', false);}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="dataSource" value="" />
<input type="text" class="dataSource" value="" /> <input type="text" class="dataSource" value="" /> <button onclick="enableDataSource()">Enable field</button>
关于javascript - 如何禁用 .select2() javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46969997/