ユーザ用ツール

サイト用ツール


プログラム言語:javascript_jquery:フォームの値_input_selectなど_の操作

文書の過去の版を表示しています。


【jQuery】フォームの値(input、selectなど)の操作

コピペして test.html などの htmlファイルとして保存すれば、そのまま動作確認が行なえます。

input の値(value)の操作

  1. <html>
  2. <meta>
  3. <meta charset="UTF-8">
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  5. </meta>
  6. <body>
  7. <input type="text" name="hoge" value="" id="input-01" class="form-hoge">
  8. <button type="button" class="form-change">change</button>
  9. <button type="button" class="form-clear">clear</button>
  10. <script>
  11. $(function() {
  12. $('.form-hoge').on('change', function(){
  13. // id で value を取得
  14. let hoge_id = $('#input-01').val();
  15. console.log(hoge_id);
  16.  
  17. // class で value を取得
  18. let hoge_class = $('.form-hoge').val();
  19. console.log(hoge_class);
  20.  
  21. // name で value を取得
  22. let hoge_name = $('input[name=hoge]').val();
  23. console.log(hoge_name);
  24. });
  25.  
  26. $('.form-change').on('click', function(){
  27. // value の変更
  28. $('.form-hoge').val('abc');
  29. });
  30.  
  31. $('.form-clear').on('click', function(){
  32. // value を空にする
  33. $('.form-hoge').val('');
  34. });
  35. });
  36. </script>
  37. </body>
  38. </html>

input(チェックボックス、ラジオボタン)の操作

  1. <html>
  2. <meta>
  3. <meta charset="UTF-8">
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  5. </meta>
  6. <body>
  7. 性別
  8. <label><input type="radio" name="figure" value="male" id="form-figure-male" class="form-figure">男性</label>
  9. <label><input type="radio" name="figure" value="female" id="form-figure-female" class="form-figure">女性</label>
  10. <button type="button" class="form-change-male">change male</button>
  11. <button type="button" class="form-change-female">change female</button>
  12. <button type="button" class="form-clear-1">clear 1</button>
  13. <button type="button" class="form-clear-2">clear 2</button>
  14. <script>
  15. $(function() {
  16. $('.form-figure').on('change', function(){
  17. // class で値(value)を取得
  18. let figure_class = $('.form-figure:checked').val();
  19. console.log(figure_class);
  20.  
  21. // name で値(value)を取得
  22. let figure_name = $('input[name=figure]:checked').val();
  23. console.log(figure_name);
  24. });
  25.  
  26. $('.form-change-male').on('click', function(){
  27. // チェック状態(checked)を val で変更
  28. $('input[name=figure]').val(['male']);
  29. });
  30.  
  31. $('.form-change-female').on('click', function(){
  32. // チェック状態(checked)を prop で変更
  33. $('#form-figure-female').prop('checked', true);
  34. });
  35.  
  36. $('.form-clear-1').on('click', function(){
  37. // チェック状態(checked)を val で解除
  38. $('input[name=figure]').val(['']);
  39. });
  40.  
  41. $('.form-clear-2').on('click', function(){
  42. // チェック状態(checked)を prop で解除
  43. $('.form-figure').prop('checked', false);
  44. });
  45. });
  46. </script>
  47. </body>
  48. </html>

コメント

コメントを入力. Wiki文法が有効です:
 
プログラム言語/javascript_jquery/フォームの値_input_selectなど_の操作.1558408494.txt.gz · 最終更新: 2019/05/21 12:14 by yusuke_komori