jQuery テキストフォームdisabledであるかを判定するサンプル
環境
Google Chrome 105.0.5195.127
Windows 10 Home 64bit
jquery 3.6.1
構文
$('指定要素’).is(':disabled’)
「is(':disabled’) 」を使用して指定要素が無効かどうか確認します。
使用例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#btnchk').on('click', function () {
//disabledであるかを判定
$('#result').text( $('#username').is(':disabled') )
})
});
</script>
</head>
<body>
<h2 id="result"></h2>
名前<input id="username" type="text" disabled>
<p>
<button id="btnchk" >確認</button></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#btnchk').on('click', function () {
//disabledであるかを判定
$('#result').text( $('#username').is(':disabled') )
})
});
</script>
</head>
<body>
<h2 id="result"></h2>
名前<input id="username" type="text" disabled>
<p>
<button id="btnchk" >確認</button></p>
</body>
</html>
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#btnchk').on('click', function () { //disabledであるかを判定 $('#result').text( $('#username').is(':disabled') ) }) }); </script> </head> <body> <h2 id="result"></h2> 名前<input id="username" type="text" disabled> <p> <button id="btnchk" >確認</button></p> </body> </html>
実行結果
「確認」ボタンをクリックすると、指定要素「id="username"」のdisabled属性が「true」を表示します。