2017-10-23 4 views
0

私はこのアプリで3つの問題があります。クリックしたり、クラス名を変更したり、ハイライトを非表示にしたりしないようにする - jQuery

最初に問題は、私たちが開いた後にアプリケーションが閉じられたときに、ショーボタンの名前が「オファーを表示」に変更されないことです。

第2のは、ユーザーが2番目と3番目のボタンをクリックすることを防止しません。彼はただ1つのオファーをクリックすることができますが、今ではユーザーはすべてのオファーをクリックすることができます。

第3のの場合、ユーザーは1回クリックすることで新しいオファーを強調表示し、次にハイライトを非表示にすることができます。助けをお待ちしています。

//// hide offer before the Dom is loaded 
 
//$('ul').hide() 
 
$(document).ready(function() { 
 
    
 
    function showHideOffer() { 
 
     $('ul').slideToggle(); 
 
    } 
 
    
 
    //click to show offers 
 
    $('.card').on('click', '.showOffers', showHideOffer, function() { 
 
     $('.showOffers').html('Hide Offers'); 
 
    }); 
 
    
 
    //click to hide offers - change name to show offers doesn't work!! 
 
    $('.card').on('click','.showOffers', showHideOfferfunction() { 
 
    $('.showOffers').html('Show Offers'); 
 
}); 
 
    
 
    // click to book, to show info and close button and span 
 
    $('li').on('click', 'button', function(){ 
 
     var offerName = $(this).closest('.tour').data('name'); 
 
     var offerPrice = $(this).closest('.tour').data('price') 
 
     var message = $('<ol class="breadcrumb"><li class="breadcrumb-item active" style="color:#3CB371">Success! You have booked '+offerName+' offer for '+offerPrice+'!</li></ol>'); 
 
     $(this).closest('.tour').append(message); 
 
     
 
     $(this).prev().closest('.details').remove(); 
 
     $(this).remove();  
 
    }); 
 
    
 
    // filter new offers by clicking a "new" button 
 
    $('#buttons').on('click','.filterNew', function() { 
 
      $('.tour').filter('.newOffer').addClass('highlightnew'); 
 
      $('.highlightpopular').removeClass('highlightpopular'); 
 
       //click second time and the highlight is hidden 
 
       $('#buttons').on('click', '.filterNew', function() { 
 
        $('.highlightnew').removeClass('highlightnew'); 
 
       }); 
 
     }); 
 
    
 
     // filter by popular offers 
 
     $('#buttons').on('click', '.filterPopular', function() { 
 
      $('.tour').filter('.popular').addClass('highlightpopular'); 
 
      $('.highlightnew').removeClass('highlightnew'); 
 
     }); 
 
    
 
    
 
});
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 

 
     <meta charset="utf-8"> 
 
     <title>GuidedTours</title> 
 
     <link rel="stylesheet" type="text/css" href="wageup.css"> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
     <style> 
 
      .selected { 
 
      color: red; 
 
      } 
 
      .highlightnew { 
 
      background: #3D9970; 
 
      } 
 
      .highlightpopular { 
 
       background: #39CCCC; 
 
      } 
 
      ul { 
 
       display:none; 
 
      } 
 
     </style> 
 
    </head> 
 
    <body> 
 
    <div class="container"> 
 
     <h2>Guided Tours</h2> 
 
      <hr> 
 
      <div id="tours" class="card"> 
 
       <!-- click to show --> 
 
       <button type="button" value="Show Offers" class="btn showOffers btn-primary btn-sm">Show Offers</button> 
 
       
 
       <ul class="list-group list-group-flush"> 
 
       <li class="usa tour newOffer list-group-item"; data-name="New York" data-price="$550"> 
 
        <h3>New York, New York</h3> 
 
        <span class="details badge badge-success">$1,899 for 7 nights</span> 
 
        <button class="book btn btn-primary">Book Now</button> 
 
       </li> 
 

 
       <li class="europe tour newOffer list-group-item" data-name="Paris" data-price="$450"> 
 
        <h3>Paris, France</h3> 
 
        <span class="details badge badge-success">$2,299 for 7 nights</span> 
 
        <button class="book btn btn-primary">Book Now</button> 
 
       </li> 
 

 
       <li class="asia tour popular list-group-item" data-name="Tokyo" data-price="$650"> 
 
        <h3>Tokyo, Japan</h3> 
 
        <span class="details badge badge-success">$3,799 for 7 nights</span> 
 
        <button class="book btn btn-primary">Book Now</button> 
 
       </li> 
 
       </ul> 
 
       <ul id="buttons" class="list-group list list-group-flush"> 
 
       <button class="filterNew btn btn-info">New</button> 
 
       <button class="filterPopular btn btn-info">Popular</button> 
 
       </ul> 
 
      </div> 
 
     </div> 
 

 

 

 

 

 
     <!-- Scripts --> 
 

 
     <!-- Jquery --> 
 
     <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> 
 
     <!-- Bootstrap --> 
 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
 
     <!-- JS --> 
 
     <script type="text/javascript" src="guidedtours.js"></script> 
 

 
    </body> 
 
</html>

+0

あなたの関数に以下の修正をしてみてください、あなただけ^それを行うには、あなたのコードを求めていないしています^ –

+0

私はあなたがそれを望むならバグを提供することができますが、クリーンなコードの目的のためにそれを削除しました。 – Adam

+0

私はもっと明確にしようとしていますが、あなたはコードを表示して再度提示のオファーを要求しているわけではありません。例えば –

答えて

0

まず問題は解決:ここにチェックアウト: 変更

$('.card').on('click', '.showOffers', showHideOffer, function() { 
     //$('.showOffers').html(''); 

     $(this).text($(this).text() == 'Hide Offers' ? "Show Offers" : "Hide Offers"); 
    }); 

の代わりにコード:

$('.card').on('click', '.showOffers', showHideOffer, function() { 
     $('.showOffers').html('Hide Offers'); 
    }); 
第二の問題については

 
 
    
 
    //click to show offers 
 
    $('.card').on('click', '.showOffers', showHideOffer, function() { 
 
     //$('.showOffers').html(''); 
 
     
 
     $(this).text($(this).text() == 'Hide Offers' ? "Show Offers" : "Hide Offers"); 
 
    }); 
 
    
 
    //click to hide offers - change name to show offers doesn't work!! 
 
    $('.card').on('click','.showOffers', showHideOffer); 
 
    
 
    // click to book, to show info and close button and span 
 
    $('li').on('click', 'button', function(){ 
 
     var offerName = $(this).closest('.tour').data('name'); 
 
     var offerPrice = $(this).closest('.tour').data('price') 
 
     var message = $('<ol class="breadcrumb"><li class="breadcrumb-item active" style="color:#3CB371">Success! You have booked '+offerName+' offer for '+offerPrice+'!</li></ol>'); 
 
     $(this).closest('.tour').append(message); 
 
     
 
     $(this).prev().closest('.details').remove(); 
 
     $(this).remove();  
 
    }); 
 
    
 
    // filter new offers by clicking a "new" button 
 
    $('#buttons').on('click','.filterNew', function() { 
 
      $('.tour').filter('.newOffer').addClass('highlightnew'); 
 
      $('.highlightpopular').removeClass('highlightpopular'); 
 
       //click second time and the highlight is hidden 
 
       $('#buttons').on('click', '.filterNew', function() { 
 
        $('.highlightnew').removeClass('highlightnew'); 
 
       }); 
 
     }); 
 
    
 
     // filter by popular offers 
 
     $('#buttons').on('click', '.filterPopular', function() { 
 
      $('.tour').filter('.popular').addClass('highlightpopular'); 
 
      $('.highlightnew').removeClass('highlightnew'); 
 
     }); 
 
    
 
    
 
function showHideOffer() { 
 
     $('ul').slideToggle(); 
 
    }
.selected { 
 
      color: red; 
 
      } 
 
      .highlightnew { 
 
      background: #3D9970; 
 
      } 
 
      .highlightpopular { 
 
       background: #39CCCC; 
 
      } 
 
      ul { 
 
       display:none; 
 
      }
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 

 

 

 
    
 
    <div class="container"> 
 
     <h2>Guided Tours</h2> 
 
      <hr> 
 
      <div id="tours" class="card"> 
 
       <!-- click to show --> 
 
       <button type="button" value="Show Offers" class="btn showOffers btn-primary btn-sm">Show Offers</button> 
 
       
 
       <ul class="list-group list-group-flush"> 
 
       <li class="usa tour newOffer list-group-item"; data-name="New York" data-price="$550"> 
 
        <h3>New York, New York</h3> 
 
        <span class="details badge badge-success">$1,899 for 7 nights</span> 
 
        <button class="book btn btn-primary">Book Now</button> 
 
       </li> 
 

 
       <li class="europe tour newOffer list-group-item" data-name="Paris" data-price="$450"> 
 
        <h3>Paris, France</h3> 
 
        <span class="details badge badge-success">$2,299 for 7 nights</span> 
 
        <button class="book btn btn-primary">Book Now</button> 
 
       </li> 
 

 
       <li class="asia tour popular list-group-item" data-name="Tokyo" data-price="$650"> 
 
        <h3>Tokyo, Japan</h3> 
 
        <span class="details badge badge-success">$3,799 for 7 nights</span> 
 
        <button class="book btn btn-primary">Book Now</button> 
 
       </li> 
 
       </ul> 
 
       <ul id="buttons" class="list-group list list-group-flush"> 
 
       <button class="filterNew btn btn-info">New</button> 
 
       <button class="filterPopular btn btn-info">Popular</button> 
 
       </ul> 
 
      </div> 
 
     </div>

+0

これは非常に賢明な解決策です。ありがとう。なぜカッコを1つの条件に入れ、2つを実行セクションに入れますか? – Adam

+0

それは私の間違いです。同じコードを書くこともできます.2回書くことはありません。実行中に重複している可能性があります。 –

+0

同じコードを入力することもできます。 – Adam

0
To Resolve your first issue modify your function like below 
var ValuesShowOrHidden = 0; 
$('.card').on('click', '.showOffers', showHideOffer, function() { 
       if (ValuesShowOrHidden == 0) { 
        $('.showOffers').html('Hide Offers'); 
        ValuesShowOrHidden = 1; 

       } 
       else if (ValuesShowOrHidden==1) { 
        $('.showOffers').html('Show Offers'); 
        ValuesShowOrHidden = 0; 
       } 
      }); 

あなたはどんなバグを持っていない

$('li').on('click', 'button', function() { 


       var offerName = $(this).closest('.tour').data('name'); 
       var offerPrice = $(this).closest('.tour').data('price') 
       var message = $('<ol class="breadcrumb"><li class="breadcrumb-item active" style="color:#3CB371">Success! You have booked ' + offerName + ' offer for ' + offerPrice + '!</li></ol>'); 
       $(this).closest('.tour').append(message); 

       $(this).prev().closest('.details').remove(); 
       $(this).remove(); 
       $('li').unbind("click"); 
      }); 
+0

2行目の問題を1行で処理する素晴らしい方法です!最初の問題でお試しいただき、ありがとうございます。 3番目の問題について考えていますか? – Adam

+0

あなたの3番目の問題は、リンクをクリックできないようにすることで既に解決されています。ユーザーはオファーを選択し、そこに緑色のメッセージを表示します。ユーザーはオファーを表示して表示することができますが、オファーは選択できません。あなたは他の何かをしたいですか? さらに、このJavaスクリプトコードは多くの汚い複雑なものです。私はちょうどあなたの問題にいくつかの迅速なソリューションを提供していますが、全体的にこのアプリケーションはこのように動作するつもりはありません。それは割り当て作業のように見えます。 あなたのニーズを満たす答えをupvoteできます。ありがとうございました。 – umer

+0

汚いと複雑なことを理解することはどういう意味ですか?どの部分? – Adam

関連する問題