我如何切换禁用的输入状态
How i can toggle the input statue disabled
在我问这个问题之前,我在 Whosebug 上搜索过这个问题,发现我可以通过 prop 或 attr 禁用或启用输入,
但是当我尝试实现这个时——就像下面的例子一样——它不想切换,我是不是错过了什么?
谢谢
$(function() {
$('#submit').on('click', function() {
$('body').prepend('<div class= "item" ></div>');
if ($('.item').length > 0) {
$('#search').removeAttr('disabled');
}
if ($('.item').length == 0) {
$('#search').attr('disabled', 'disabled');
//$('#search').prop('disabled', true);
}
});
$('#reset').on('click', function() {
$('.item').remove();
})
});
button {
display: inline-block
}
input {
position: fixed;
bottom: 0;
right: 0
}
.item {
width: 50px;
height: 50px;
background: orangered;
margin: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='search' id='search' disabled>
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>
您不需要检查生成的 div 的长度,只需根据点击情况禁用 true 或 false:
$(function() {
$("input").prop('disabled', true);
$('#submit').on('click', function() {
$('body').prepend('<div class= "item" ></div>');
$("input").prop('disabled', false);
});
$('#reset').on('click', function() {
$('.item').remove();
$("input").prop('disabled', true);
})
});
button {
display: inline-block
}
input {
position: fixed;
bottom: 0;
right: 0
}
.item {
width: 50px;
height: 50px;
background: orangered;
margin: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='search' id='search'>
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>
对于你的HTML,使用这个:
<input type='search' id='search' disabled="disabled">
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>
请注意 [disabled="disabled"],然后您的 JavaScript 使用:
<script>
(function() {
var searchInput = $("#search");
var searchAttr = $("#search").attr("disabled");
$( "#submit" ).click(function() {
$('body').prepend('<div class= "item" ></div>');
if ( $('.item').length === 0 ) { // Use triple equals for exact type check
searchInput.attr( "disabled", searchAttr );
} else{
searchInput.removeAttr("disabled");
}
});
$('#reset').on('click', function() {
$('.item').remove();
searchInput.attr( "disabled", searchAttr );
})
})();
</script>
这应该有效。
在我问这个问题之前,我在 Whosebug 上搜索过这个问题,发现我可以通过 prop 或 attr 禁用或启用输入, 但是当我尝试实现这个时——就像下面的例子一样——它不想切换,我是不是错过了什么? 谢谢
$(function() {
$('#submit').on('click', function() {
$('body').prepend('<div class= "item" ></div>');
if ($('.item').length > 0) {
$('#search').removeAttr('disabled');
}
if ($('.item').length == 0) {
$('#search').attr('disabled', 'disabled');
//$('#search').prop('disabled', true);
}
});
$('#reset').on('click', function() {
$('.item').remove();
})
});
button {
display: inline-block
}
input {
position: fixed;
bottom: 0;
right: 0
}
.item {
width: 50px;
height: 50px;
background: orangered;
margin: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='search' id='search' disabled>
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>
您不需要检查生成的 div 的长度,只需根据点击情况禁用 true 或 false:
$(function() {
$("input").prop('disabled', true);
$('#submit').on('click', function() {
$('body').prepend('<div class= "item" ></div>');
$("input").prop('disabled', false);
});
$('#reset').on('click', function() {
$('.item').remove();
$("input").prop('disabled', true);
})
});
button {
display: inline-block
}
input {
position: fixed;
bottom: 0;
right: 0
}
.item {
width: 50px;
height: 50px;
background: orangered;
margin: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='search' id='search'>
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>
对于你的HTML,使用这个:
<input type='search' id='search' disabled="disabled">
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>
请注意 [disabled="disabled"],然后您的 JavaScript 使用:
<script>
(function() {
var searchInput = $("#search");
var searchAttr = $("#search").attr("disabled");
$( "#submit" ).click(function() {
$('body').prepend('<div class= "item" ></div>');
if ( $('.item').length === 0 ) { // Use triple equals for exact type check
searchInput.attr( "disabled", searchAttr );
} else{
searchInput.removeAttr("disabled");
}
});
$('#reset').on('click', function() {
$('.item').remove();
searchInput.attr( "disabled", searchAttr );
})
})();
</script>
这应该有效。