お世話になります。2種のプラグインを取り入れてある制作物を作っているのですが、上手くいかずご教示願いたく投稿させて頂きました。
フォームで入力したデータをDBに登録する部分ですが、フォームを必要に応じて行を増やして登録したく、上記サイトを参考にしたのが以下ノースです。
<!doctype html> <html lang="ja"> <head> <meta charset="utf-8"> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(document).on("click", ".add", function() { $(this).parent().clone(true).insertAfter($(this).parent()); }); $(document).on("click", ".del", function() { var target = $(this).parent(); if (target.parent().children().length > 1) { target.remove(); } }); </script> <style type="text/css"> <!-- #input_plural { margin: 10px 0; } #input_plural input.form-control1 { display: inline-block; width: 40%; height: 34px; padding: 6px 12px; font-size: 14px; color: #555; } #input_plural input.form-control2 { display: inline-block; width: 40%; height: 34px; padding: 6px 12px; font-size: 14px; color: #555; } #input_plural input.pluralBtn { width: 30px; height: 30px; border: 1px solid #ccc; background: #fff; border-radius: 5px; padding: 0; margin: 0; } --> </style> </head> <body> <form> <div id="input_pluralBox"> <div id="input_plural"> <input type="text" value="" name="karikamoku" class="karikamokuTags"> <input type="text" value="" name="torihkisaki" class="torihkisakiTags"> <input type="button" value="+" class="add pluralBtn"> <input type="button" value="-" class="del pluralBtn"> </div> </div> </form> </body> </html>
また、入力するデータは決められたワードを入力したいため、タグ形式で入力するものとし
プラグイン( http://aehlke.github.io/tag-it/ )を利用させていただき用意したものが以下のソースです。
<!doctype html> <html lang="ja"> <head> <meta charset="utf-8"> <title></title> <!--tagit--> <script src="https://code.jquery.com/jquery-1.9.1.js"></script> <link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script> <link rel="stylesheet" type="text/css" href="tagit/css/jquery.tagit.css"> <script type="text/javascript" src="tagit/js/tag-it.min.js"></script> <script type="text/javascript"> $(function() { var tagList = ['現金','現金過不足','当座預金','普通預金','定期預金','その他預金']; $('.karikamokuTags').tagit({ removeConfirmation: true, availableTags: tagList, fieldName: "karikamoku", tagLimit:1 }); var tagList = ['売掛金','未収賃貸料','有価証券','商品','棚卸資産','前払金','仮払金']; $('.torihkisakiTags').tagit({ removeConfirmation: true, availableTags: tagList, fieldName: "torihkisaki", tagLimit:1 }); }); </script> <!--tagit--> </head> <body> <div><input type="text" class="karikamokuTags" value="" name="karikamoku"></div> <div><input type="text" class="torihkisakiTags" value="" name="torihkisaki"></div> </body> </html>
この2つのソースを参考に、
のように、フォームにタグを入力し、フォームを追加、そしてそのフォームにタグを入力、、、という流れをイメージしています。
この2つを組み合わせたソースがこちら。
<!doctype html> <html lang="ja"> <head> <meta charset="utf-8"> <title></title> <!--tagit--> <script src="https://code.jquery.com/jquery-1.9.1.js"></script> <link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script> <link rel="stylesheet" type="text/css" href="tagit/css/jquery.tagit.css"> <script type="text/javascript" src="tagit/js/tag-it.min.js"></script> <script type="text/javascript"> $(function() { var tagList = ['現金','現金過不足','当座預金','普通預金','定期預金','その他預金']; $('.karikamokuTags').tagit({ removeConfirmation: true, availableTags: tagList, fieldName: "karikamoku", tagLimit:1 }); var tagList = ['売掛金','未収賃貸料','有価証券','商品','棚卸資産','前払金','仮払金']; $('.torihkisakiTags').tagit({ removeConfirmation: true, availableTags: tagList, fieldName: "torihkisaki", tagLimit:1 }); }); </script> <!--tagit--> <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> --> <script type="text/javascript"> $(document).on("click", ".add", function() { $(this).parent().clone(true).insertAfter($(this).parent()); }); $(document).on("click", ".del", function() { var target = $(this).parent(); if (target.parent().children().length > 1) { target.remove(); } }); </script> <style type="text/css"> <!-- #input_plural { margin: 10px 0; } #input_plural input.form-control1 { display: inline-block; width: 40%; height: 34px; padding: 6px 12px; font-size: 14px; color: #555; } #input_plural input.form-control2 { display: inline-block; width: 40%; height: 34px; padding: 6px 12px; font-size: 14px; color: #555; } #input_plural input.pluralBtn { width: 30px; height: 30px; border: 1px solid #ccc; background: #fff; border-radius: 5px; padding: 0; margin: 0; } --> </style> </head> <body> <form> <div id="input_pluralBox"> <div id="input_plural"> <input type="text" value="" name="karikamoku" class="karikamokuTags"> <input type="text" value="" name="torihkisaki" class="torihkisakiTags"> <input type="button" value="+" class="add pluralBtn"> <input type="button" value="-" class="del pluralBtn"> </div> </div> </form> </body> </html>
しかし、このフォームでは以下の画像のように、思うような動きをしてくれません。
大変お恥ずかしい質問と恐縮していますが、javascriptの理解がないに等しく、今日1日中も図書館へ行き、本を片手に原因を探っていたのですが、そもそも理解がないため、原因を突き止めるまで至りませんでした。
お忙しい中恐縮ですが、どこが原因なのかご教示願うことは出来ませんでしょう。
よろしくお願いいたします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/22 02:56
2020/01/22 06:16