時間を入力する際jsで値を変えたい
htmlのinputのフォームで
入力時は【1200】 選択が外れると【12:00】にしたいです。
【200】なども【02:00】にしたいです。
その他時間ではない数字や文字がはいったときは空白にするつもりです。
またできれば、
全角数字全角【:】にも対応したいです
該当のソースコード
html
1<section class="content"> 2 <form method="post"> 3 <div> 4 <label for="time">時間</label> 5 <input type="text" id="time" name="time" value="time" placeholder=""> 6 </div> 7</section>
js
1 2 let $time = $('#time'); 3 4 $time.focusin(function () { 5 let value = $(this).val().replace(":", ""); 6 $(this).val(value); 7 }); 8 9 10 $time.focusout(function () { 11 if(!isNaN(time)){ 12 $(this).val(""); 13 } 14 time.value = strIns(time.value, 2, ':'); 15 }); 16 17 function strIns(str, idx, val){ 18 return str.slice(0, idx) + val + str.slice(idx); 19 }
フォーカスインで外れるのは確認できたのですが
アウトでうまく:がついてくれません。
ご教授よろしくお願いします。
補足情報
pythonとhtmlとjq使用しています。
回答1件
あなたの回答
tips
プレビュー