\r\n \r\n\r\n\r\n```\r\n```css\r\n@charset \"UTF-8\";\r\n\r\nhtml {\r\n height: 100%;\r\n}\r\nbody {\r\n min-height: 100%;\r\n color: #333;\r\n background-color: #fafafa;\r\n}\r\n.container {\r\n}\r\n.header {\r\n width: 100%;\r\n background-color: #43cee0;\r\n}\r\n.header__title {\r\n line-height: 60px;\r\n text-align: center;\r\n font-size: 20px;\r\n color: #fff;\r\n}\r\n.search {\r\n overflow: hidden;\r\n margin-bottom: 50px;\r\n background: #fff;\r\n box-shadow: 0 1px 5px #ccc;\r\n}\r\n.search__text {\r\n width: 100%;\r\n}\r\n.search__text__input {\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n width: 100%;\r\n margin-bottom: 20px;\r\n padding: 0 10px;\r\n line-height: 3em;\r\n border-top: none;\r\n border-left: none;\r\n border-right: none;\r\n border-bottom: 1px solid #eee;\r\n font-size: 20px;\r\n}\r\n.search__btn {\r\n display: block;\r\n margin: 0 auto 20px;\r\n padding: 0 20px;\r\n line-height: 40px;\r\n border: none;\r\n font-size: 18px;\r\n color: #fff;\r\n background-color: #43cee0;\r\n}\r\n.search__btn:hover {\r\n background-color: #1eabbd;\r\n}\r\n.lists {\r\n overflow: hidden;\r\n margin-left: 20px;\r\n \r\n}\r\n.lists__item {\r\n -webkit-box-sizing: border-box;\r\n box-sizing: border-box;\r\n display: inline-block;\r\n width: 25%;\r\n padding: 0 20px 20px 0;\r\n vertical-align: top;\r\n text-align: center;\r\n}\r\n.lists__item__inner {\r\n width: 100%;\r\n max-width: 200px;\r\n margin: 0 auto;\r\n}\r\n.lists__item__link {\r\n display: block;\r\n text-decoration: none;\r\n}\r\n.lists__item__img {\r\n margin-bottom: 20px;\r\n width: 100%;\r\n max-width: 150px;\r\n -webkit-box-shadow: 0 1px 5px #ccc;\r\n box-shadow: 0 1px 5px #ccc;\r\n}\r\n.lists__item__detail {\r\n margin-bottom: 10px;\r\n padding-left: 5em;\r\n text-indent: -5em;\r\n line-height: 1.5em;\r\n text-align: left;\r\n font-size: 12px;\r\n}\r\n.message {\r\n margin-bottom: 50px;\r\n text-align: center;\r\n}\r\n\r\n@media screen and (max-width: 767px) {\r\n .lists__item {\r\n width: 33.33%;\r\n }\r\n}\r\n\r\n@media screen and (max-width: 479px) {\r\n .lists__item {\r\n width: 50%;\r\n }\r\n}\r\n```\r\n```javascript\r\n$(function(){\r\n $('#js-search-button').on('click',function(){\r\n var keyword = $('#js-search-word').val();\r\n \r\n $.get('https://app.rakuten.co.jp/services/api/BooksTotal/Search/20170404?',{\r\n applicationId: \"1096525417627568230\",\r\n keyword: keyword,\r\n booksGenreId: '001',\r\n hits: '20',\r\n },\r\n function(data){\r\n if (data.count > 0){\r\n $('ul').empty();\r\n $('.coment').css('display','none');\r\n $.each(data.Items,function(i,item){\r\n // console.log(item);\r\n var list = $('
  • ' + '
    ' + '' + '\"'' +\r\n '

    作品名: ' + item.Item.title + '

    ' +\r\n '

    作者 : ' + item.Item.author + '

    ' +\r\n '

    出版社: ' + item.Item.publisherName + '

    ' + '
    ' +\r\n '
    ' + '
  • ');\r\n $('ul').append(list);\r\n })\r\n }\r\n if (data.count === 0) {\r\n $('ul').empty();\r\n $('ul').before('
    ')\r\n coment = $('.coment');\r\n coment.html('

    検索結果が見つかりませんでした。
    別のキーワードで検索して下さい。

    ');\r\n }\r\n });\r\n });\r\n });\r\n```\r\n###補足情報(言語/FW/ツール等のバージョンなど)\r\n検索がなかなかうまくいかず詰まってしまいました。\r\njqueryの知識を深めるためにもアドバイスよろしくお願いします。","answerCount":2,"upvoteCount":0,"datePublished":"2018-01-22T02:47:26.523Z","dateModified":"2018-01-22T02:47:26.523Z","acceptedAnswer":{"@type":"Answer","text":"楽天のAPIドキュメントを見てみると、`page`というパラメータが利用できるようです。\r\n\r\n**楽天ウェブサービス: 楽天ブックス総合検索API(version:2017-04-04) | API一覧**\r\n[https://webservice.rakuten.co.jp/index.php/api/bookstotalsearch/](https://webservice.rakuten.co.jp/index.php/api/bookstotalsearch/)\r\n\r\n`$.get()`の中で`keyword`などと一緒に`page`でページ番号を渡すようにすると、2ページ目以降のデータを取得できるようになると思います。\r\n\r\n2ページ目の結果を1ページ目の後ろに追加するには、`$('ul').empty();`としなければ、`$('ul').append(list);`だけでOKですね。\r\n\r\n### 追記\r\n\r\n`.on('click',〜)`の外側で`var pageNum = 0;`と変数を定義し、`.on('click',〜)`の内側で`pageNum = pageNum + 1;`とすることで、ボタンがクリックされる度にページ番号が1ずつ増加します。\r\nあとは`page`パラメータに`page: pageNum`のようにしてこれを渡すことで、続けて次のページをリクエストできるようになります。","dateModified":"2018-01-22T06:50:47.517Z","datePublished":"2018-01-22T04:06:10.397Z","upvoteCount":3,"url":"https://teratail.com/questions/110073#reply-167934"},"suggestedAnswer":[{"@type":"Answer","text":"ロジックが組み上がっていると思いますので後はプログラム化するだけかと\r\n1.検索ボタンを押した時はulに結果を追加\r\n2.他の検索ワードを入力したとき結果をクリア","dateModified":"2018-01-22T04:04:29.954Z","datePublished":"2018-01-22T04:04:29.954Z","upvoteCount":1,"url":"https://teratail.com/questions/110073#reply-167933","comment":[{"@type":"Comment","text":"ありがとうございます!\r\n動きの順序を意識してやってみます!","datePublished":"2018-01-22T05:26:21.053Z","dateModified":"2018-01-22T05:26:21.053Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/CSS3","name":"CSS3に関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/110073","name":"jQuery 同じキーワードで検索するたびにデータを追加していきたい"}}]}}}
    質問するログイン新規登録

    Q&A

    解決済

    2回答

    3892閲覧

    jQuery 同じキーワードで検索するたびにデータを追加していきたい

    ysmd

    総合スコア17

    CSS3

    CSS(Cascading Style Sheet)の第3版です。CSS3と略されることが多いです。色やデザインを柔軟に変更することが可能になります。

    HTML5

    HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

    JavaScript

    JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

    jQuery

    jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

    0グッド

    1クリップ

    投稿2018/01/22 02:47

    0

    1

    ###前提・実現したいこと
    楽天apiを使い、キーワードで検索して結果が出た状態で、もう一度検索ボタンを押すとデータが追加されて表示されるようにしたいです。
    一回の検索で20件商品が表示され、もう一度検索ボタンを押すと降順で20件追加されてどんどん増えて行く仕様を目指しています。
    ただ、この動きは同一のキーワードの時のみで、途中で他のキーワードで検索したら結果はクリアされてまた20件新たに出るようにしたいです。
    ###発生している問題・エラーメッセージ
    現在は同一キーワードで検索しても、また最初からデータが取得されて次のデータを取得できない状態です。

    ###該当のソースコード

    html

    1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title>api test</title> 6 <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> 7 <link rel="stylesheet" href="css/reset.css"> 8 <link rel="stylesheet" href="css/base.css"> 9 <link rel="stylesheet" href="css/style.css"> 10</head> 11<body> 12 <div class="wrap"> 13 <div class="container"> 14 <div class="header"> 15 <p class="header__title">Search Books!</p> 16 </div> 17 <div class="search"> 18 <div class="search__text"> 19 <input type="text" id="js-search-word" class="search__text__input" value="" placeholder="検索する"> 20 </div> 21 <button id="js-search-button" class="search__btn">検索する</button> 22 </div> 23 <ul class="lists"> 24 25 </ul> 26 </div> 27 </div> 28 <script src="js/jquery.js"></script> 29 <script type="text/javascript"> 30 //ここにjQueryが入ります 31 </script> 32</body> 33</html>

    css

    1@charset "UTF-8"; 2 3html { 4 height: 100%; 5} 6body { 7 min-height: 100%; 8 color: #333; 9 background-color: #fafafa; 10} 11.container { 12} 13.header { 14 width: 100%; 15 background-color: #43cee0; 16} 17.header__title { 18 line-height: 60px; 19 text-align: center; 20 font-size: 20px; 21 color: #fff; 22} 23.search { 24 overflow: hidden; 25 margin-bottom: 50px; 26 background: #fff; 27 box-shadow: 0 1px 5px #ccc; 28} 29.search__text { 30 width: 100%; 31} 32.search__text__input { 33 -webkit-box-sizing: border-box; 34 box-sizing: border-box; 35 width: 100%; 36 margin-bottom: 20px; 37 padding: 0 10px; 38 line-height: 3em; 39 border-top: none; 40 border-left: none; 41 border-right: none; 42 border-bottom: 1px solid #eee; 43 font-size: 20px; 44} 45.search__btn { 46 display: block; 47 margin: 0 auto 20px; 48 padding: 0 20px; 49 line-height: 40px; 50 border: none; 51 font-size: 18px; 52 color: #fff; 53 background-color: #43cee0; 54} 55.search__btn:hover { 56 background-color: #1eabbd; 57} 58.lists { 59 overflow: hidden; 60 margin-left: 20px; 61 62} 63.lists__item { 64 -webkit-box-sizing: border-box; 65 box-sizing: border-box; 66 display: inline-block; 67 width: 25%; 68 padding: 0 20px 20px 0; 69 vertical-align: top; 70 text-align: center; 71} 72.lists__item__inner { 73 width: 100%; 74 max-width: 200px; 75 margin: 0 auto; 76} 77.lists__item__link { 78 display: block; 79 text-decoration: none; 80} 81.lists__item__img { 82 margin-bottom: 20px; 83 width: 100%; 84 max-width: 150px; 85 -webkit-box-shadow: 0 1px 5px #ccc; 86 box-shadow: 0 1px 5px #ccc; 87} 88.lists__item__detail { 89 margin-bottom: 10px; 90 padding-left: 5em; 91 text-indent: -5em; 92 line-height: 1.5em; 93 text-align: left; 94 font-size: 12px; 95} 96.message { 97 margin-bottom: 50px; 98 text-align: center; 99} 100 101@media screen and (max-width: 767px) { 102 .lists__item { 103 width: 33.33%; 104 } 105} 106 107@media screen and (max-width: 479px) { 108 .lists__item { 109 width: 50%; 110 } 111}

    javascript

    1$(function(){ 2 $('#js-search-button').on('click',function(){ 3 var keyword = $('#js-search-word').val(); 4 5 $.get('https://app.rakuten.co.jp/services/api/BooksTotal/Search/20170404?',{ 6 applicationId: "1096525417627568230", 7 keyword: keyword, 8 booksGenreId: '001', 9 hits: '20', 10 }, 11 function(data){ 12 if (data.count > 0){ 13 $('ul').empty(); 14 $('.coment').css('display','none'); 15 $.each(data.Items,function(i,item){ 16 // console.log(item); 17 var list = $('<li class="lists__item">' + '<div class="lists__item__inner">' + '<a href="' + 18 item.Item.itemUrl + '"class="lists__item__link" target="_blank">' + '<img src="' + item.Item 19 .largeImageUrl + '" class="lists__item__img" alt="' + item.Item.title + '">' + 20 '<p class="lists__item__detail">作品名: ' + item.Item.title + '</p>' + 21 '<p class="lists__item__detail">作者 : ' + item.Item.author + '</p>' + 22 '<p class="lists__item__detail">出版社: ' + item.Item.publisherName + '</p>' + '</a>' + 23 '</div>' + '</li>'); 24 $('ul').append(list); 25 }) 26 } 27 if (data.count === 0) { 28 $('ul').empty(); 29 $('ul').before('<div class="coment"></div>') 30 coment = $('.coment'); 31 coment.html('<p class="message">検索結果が見つかりませんでした。<br>別のキーワードで検索して下さい。</p>'); 32 } 33 }); 34 }); 35 });

    ###補足情報(言語/FW/ツール等のバージョンなど)
    検索がなかなかうまくいかず詰まってしまいました。
    jqueryの知識を深めるためにもアドバイスよろしくお願いします。

    気になる質問をクリップする

    クリップした質問は、後からいつでもMYページで確認できます。

    またクリップした質問に回答があった際、通知やメールを受け取ることができます。

    バッドをするには、ログインかつ

    こちらの条件を満たす必要があります。

    guest

    回答2

    0

    ベストアンサー

    楽天のAPIドキュメントを見てみると、pageというパラメータが利用できるようです。

    楽天ウェブサービス: 楽天ブックス総合検索API(version:2017-04-04) | API一覧
    https://webservice.rakuten.co.jp/index.php/api/bookstotalsearch/

    $.get()の中でkeywordなどと一緒にpageでページ番号を渡すようにすると、2ページ目以降のデータを取得できるようになると思います。

    2ページ目の結果を1ページ目の後ろに追加するには、$('ul').empty();としなければ、$('ul').append(list);だけでOKですね。

    追記

    .on('click',〜)の外側でvar pageNum = 0;と変数を定義し、.on('click',〜)の内側でpageNum = pageNum + 1;とすることで、ボタンがクリックされる度にページ番号が1ずつ増加します。
    あとはpageパラメータにpage: pageNumのようにしてこれを渡すことで、続けて次のページをリクエストできるようになります。

    投稿2018/01/22 04:06

    編集2018/01/22 06:50
    mooey

    総合スコア200

    バッドをするには、ログインかつ

    こちらの条件を満たす必要があります。

    ysmd

    2018/01/22 06:06

    ありがとうございます! page: '2'のようにしたら2ページ目のデータは取得できました! ただ動きとしては2ページ目のデータを取得する形でした。 検索→p1→検索→p2→検索→p3と言う挙動にするにはどうするべきでしょうか?
    mooey

    2018/01/22 06:51

    回答に追記しました。クリックの度に増加する変数を使ってページ番号をリクエストすると良いです。
    ysmd

    2018/01/22 07:54

    再度のご回答ありがとうございます! 無事クリックのたびに次のページを表示させることができました!
    guest

    0

    ロジックが組み上がっていると思いますので後はプログラム化するだけかと
    1.検索ボタンを押した時はulに結果を追加
    2.他の検索ワードを入力したとき結果をクリア

    投稿2018/01/22 04:04

    sousuke

    総合スコア3830

    バッドをするには、ログインかつ

    こちらの条件を満たす必要があります。

    ysmd

    2018/01/22 05:26

    ありがとうございます! 動きの順序を意識してやってみます!
    guest

    あなたの回答

    tips

    太字

    斜体

    打ち消し線

    見出し

    引用テキストの挿入

    コードの挿入

    リンクの挿入

    リストの挿入

    番号リストの挿入

    表の挿入

    水平線の挿入

    プレビュー

    15分調べてもわからないことは
    teratailで質問しよう!

    ただいまの回答率
    85.29%

    質問をまとめることで
    思考を整理して素早く解決

    テンプレート機能で
    簡単に質問をまとめる

    質問する

    関連した質問