2017-04-26 16 views
-2

"Show Picture"ボタンをクリックすると、画像がフェードインし、家の写真が表示されます。何らかの理由で私は私の人生のために働くことができません。また、フォームをクリアするためにリセットボタンが機能するように助けてください。jsが動作しない理由を理解できません

$(document).ready(function(){ 
 

 
$(".showA").click(function() { 
 
    $("#house1").fadeIn("slow"); 
 
});
img 
 
{ 
 
\t padding:29px; 
 
\t opacity:0; 
 
\t width:250px; 
 
\t height:250px; 
 
} 
 

 
div 
 
{ 
 
    vertical-align:top; 
 
    width:267px; 
 
    height:auto; 
 
    display:inline-block; 
 
    padding:20px; 
 
    margin:0px;  
 
    border-radius:10px; 
 
    border:1px solid; 
 
} 
 

 
p 
 
{ 
 
\t margin:5px; 
 
\t padding:3px; 
 
\t background-color:blue; 
 
\t border-radius:8px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta charset="UTF-8" /> 
 
\t \t <link rel="stylesheet" type="text/css" href="css/finalproject.css"> 
 
\t \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
\t \t <script type="text/javascript" src="js/finalproject.js"> </script> 
 
\t \t <title>Final Project</title> 
 
\t </head> 
 
\t 
 
\t <body> 
 
\t \t <center><h1>Vacation Rental</h1></center> 
 
\t \t 
 
\t \t <div><form id="chatham"> 
 
     <label><b><u>Chatham, MA</u></b></label><br> 
 
     <label><b>$300 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameA" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailA" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneA" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
     <input id="showA" type="button" value="Show Picture"> 
 
     <input id="submitA" type="button" value="Submit"> 
 
     <input id="resetA" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="wellfleet"> 
 
     <label><b><u>Wellfleet, MA</u></b></label><br> 
 
     <label><b>$250 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameB" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailB" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneB" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showB" type="button" value="Show Picture"> 
 
     <input id="submitB" type="button" value="Submit"> 
 
     <input id="resetB" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="dennis"> 
 
     <label><b><u>Dennis, MA</u></b></label><br> 
 
     <label><b>$350 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameC" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailC" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneC" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showC" type="button" value="Show Picture"> 
 
     <input id="submitC" type="button" value="Submit"> 
 
     <input id="resetC" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="provincetown"> 
 
     <label><b><u>Provincetown, MA</u></b></label><br> 
 
     <label><b>$300 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameD" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailD" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneD" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showD" type="button" value="Show Picture"> 
 
     <input id="submitD" type="button" value="Submit"> 
 
     <input id="resetD" type="button" value="Reset"> 
 
     </form></div> 
 
    
 
\t <img id="house1" src="images/chatham.jpg" /> 
 
\t <img id="house2" src="images/wellfleet.jpg" /> 
 
\t <img id="house3" src="images/dennis.jpg" /> 
 
\t <img id="house4" src="images/provincetown.jpg" /> 
 
\t </body> 
 
</html>

+0

の可能性のある重複した[どのように私は私のJavaScriptコードをデバッグすることができますか?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-コード) – Liam

+0

JSコードを実行しているとき。私はクロムにインスペクタ( 'ctrl + shift + i')を開いておくことをお勧めします。 JSが失敗した場合、デバッグを容易にする相対的なエラーが表示されます。さらに、あなたが書いている間に潜在的な構文エラー(カッコがないあなたのような)を示す良い開発環境(テキストエディタ/オンラインエディタ)を見てください。 – Francisco

答えて

0

あなたは従う見、いくつかの括弧を閉じるために欠場:

$(document).ready(function(){ 

$(".showA").click(function() { 
    $("#house1").fadeOut("slow"); 
    }); 
}); //<---- I've added this line! 

$(document).ready(function(){ 
 

 
$(".showA").click(function() { 
 
    $("#house1").fadeOut("slow"); 
 
    }); 
 
});
img 
 
{ 
 
\t padding:29px; 
 
\t opacity:0; 
 
\t width:250px; 
 
\t height:250px; 
 
} 
 

 
div 
 
{ 
 
    vertical-align:top; 
 
    width:267px; 
 
    height:auto; 
 
    display:inline-block; 
 
    padding:20px; 
 
    margin:0px;  
 
    border-radius:10px; 
 
    border:1px solid; 
 
} 
 

 
p 
 
{ 
 
\t margin:5px; 
 
\t padding:3px; 
 
\t background-color:blue; 
 
\t border-radius:8px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta charset="UTF-8" /> 
 
\t \t <link rel="stylesheet" type="text/css" href="css/finalproject.css"> 
 
\t \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
\t \t <script type="text/javascript" src="js/finalproject.js"> </script> 
 
\t \t <title>Final Project</title> 
 
\t </head> 
 
\t 
 
\t <body> 
 
\t \t <center><h1>Vacation Rental</h1></center> 
 
\t \t 
 
\t \t <div><form id="chatham"> 
 
     <label><b><u>Chatham, MA</u></b></label><br> 
 
     <label><b>$300 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameA" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailA" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneA" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
     <input id="showA" type="button" value="Show Picture"> 
 
     <input id="submitA" type="button" value="Submit"> 
 
     <input id="resetA" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="wellfleet"> 
 
     <label><b><u>Wellfleet, MA</u></b></label><br> 
 
     <label><b>$250 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameB" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailB" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneB" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showB" type="button" value="Show Picture"> 
 
     <input id="submitB" type="button" value="Submit"> 
 
     <input id="resetB" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="dennis"> 
 
     <label><b><u>Dennis, MA</u></b></label><br> 
 
     <label><b>$350 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameC" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailC" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneC" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showC" type="button" value="Show Picture"> 
 
     <input id="submitC" type="button" value="Submit"> 
 
     <input id="resetC" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="provincetown"> 
 
     <label><b><u>Provincetown, MA</u></b></label><br> 
 
     <label><b>$300 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameD" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailD" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneD" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showD" type="button" value="Show Picture"> 
 
     <input id="submitD" type="button" value="Submit"> 
 
     <input id="resetD" type="button" value="Reset"> 
 
     </form></div> 
 
    
 
\t <img id="house1" src="images/chatham.jpg" /> 
 
\t <img id="house2" src="images/wellfleet.jpg" /> 
 
\t <img id="house3" src="images/dennis.jpg" /> 
 
\t <img id="house4" src="images/provincetown.jpg" /> 
 
\t </body> 
 
</html>

+0

ありがとうございますが、残念ながら関数を実行していません。 – kylebuzzy

0

問題は、あなたのコード内であなたはをやっているということですの代わりに$('#showA')というID属性がありますので、画像を表示するにはopacityを変更する必要があることを確認してください。

$('#showA').on('click', function() { 
 
    $('#house1').css('opacity', 1).fadeIn('slow'); 
 
});
img { 
 
    padding:29px; 
 
    opacity:0; 
 
    width:250px; 
 
    height:250px; 
 
} 
 

 
div { 
 
    vertical-align:top; 
 
    width:267px; 
 
    height:auto; 
 
    display:inline-block; 
 
    padding:20px; 
 
    margin:0px;  
 
    border-radius:10px; 
 
    border:1px solid; 
 
} 
 

 
p { 
 
    margin:5px; 
 
    padding:3px; 
 
    background-color:blue; 
 
    border-radius:8px; 
 
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> 
 
</script> 
 
<center> 
 
    <h1>Vacation Rental</h1> 
 
</center> 
 

 
<div> 
 
    <form id="chatham"> 
 
    <label><b><u>Chatham, MA</u></b></label><br> 
 
    <label><b>$300 a Day</b></label><br> 
 
    <label>Name:</label><br> 
 
    <input name="nameA" type="text" /><br> 
 
    <label>Email Address:</label><br> 
 
    <input name="emailA" type="text" /><br> 
 
    <label>Phone Number:</label><br> 
 
    <input name="phoneA" type="text" /><br> 
 
    <label>How many days would you like to rent?</label><br> 
 
    <select> 
 
     <option value="5-10">5-10 Days</option> 
 
     <option value="6-7">10-15 Days</option> 
 
     <option value="8-9">15-20 Days</option> 
 
    </select> 
 
    <input id="showA" type="button" value="Show Picture"> 
 
    <input id="submitA" type="button" value="Submit"> 
 
    <input id="resetA" type="button" value="Reset"> 
 
    </form> 
 
</div> 
 

 
<div> 
 
    <form id="wellfleet"> 
 
    <label><b><u>Wellfleet, MA</u></b></label><br> 
 
    <label><b>$250 a Day</b></label><br> 
 
    <label>Name:</label><br> 
 
    <input name="nameB" type="text" /><br> 
 
    <label>Email Address:</label><br> 
 
    <input name="emailB" type="text" /><br> 
 
    <label>Phone Number:</label><br> 
 
    <input name="phoneB" type="text" /><br> 
 
    <label>How many days would you like to rent?</label><br> 
 
    <select> 
 
     <option value="5-10">5-10 Days</option> 
 
     <option value="6-7">10-15 Days</option> 
 
     <option value="8-9">15-20 Days</option> 
 
    </select> 
 
    <input id="showB" type="button" value="Show Picture"> 
 
    <input id="submitB" type="button" value="Submit"> 
 
    <input id="resetB" type="button" value="Reset"> 
 
    </form> 
 
</div> 
 

 
<div> 
 
    <form id="dennis"> 
 
    <label><b><u>Dennis, MA</u></b></label><br> 
 
    <label><b>$350 a Day</b></label><br> 
 
    <label>Name:</label><br> 
 
    <input name="nameC" type="text" /><br> 
 
    <label>Email Address:</label><br> 
 
    <input name="emailC" type="text" /><br> 
 
    <label>Phone Number:</label><br> 
 
    <input name="phoneC" type="text" /><br> 
 
    <label>How many days would you like to rent?</label><br> 
 
    <select> 
 
     <option value="5-10">5-10 Days</option> 
 
     <option value="6-7">10-15 Days</option> 
 
     <option value="8-9">15-20 Days</option> 
 
    </select> 
 
    <input id="showC" type="button" value="Show Picture"> 
 
    <input id="submitC" type="button" value="Submit"> 
 
    <input id="resetC" type="button" value="Reset"> 
 
    </form> 
 
</div> 
 

 
<div> 
 
    <form id="provincetown"> 
 
    <label><b><u>Provincetown, MA</u></b></label><br> 
 
    <label><b>$300 a Day</b></label><br> 
 
    <label>Name:</label><br> 
 
    <input name="nameD" type="text" /><br> 
 
    <label>Email Address:</label><br> 
 
    <input name="emailD" type="text" /><br> 
 
    <label>Phone Number:</label><br> 
 
    <input name="phoneD" type="text" /><br> 
 
    <label>How many days would you like to rent?</label><br> 
 
    <select> 
 
     <option value="5-10">5-10 Days</option> 
 
     <option value="6-7">10-15 Days</option> 
 
     <option value="8-9">15-20 Days</option> 
 
    </select> 
 
    <input id="showD" type="button" value="Show Picture"> 
 
    <input id="submitD" type="button" value="Submit"> 
 
    <input id="resetD" type="button" value="Reset"> 
 
    </form> 
 
</div> 
 

 
<img id="house1" src="http://placehold.it/350x150" /> 
 
<img id="house2" src="http://placehold.it/350x150" /> 
 
<img id="house3" src="http://placehold.it/350x150" /> 
 
<img id="house4" src="http://placehold.it/350x150" />

+0

ありがとう!ボタンをもう一度押すと元に戻って消えるようにする手助けをするチャンスはありますか?これは学校向けのプロジェクトで、If/Elseステートメントを使用する必要があります。この状況でこのステートメントを使用できることは間違いありません。 – kylebuzzy

+0

元の質問はあなたのコードを動作させることでした。 'if/else'文を追加する必要はありません。正しい答えを選択できますか? –

0

さて、あなたのコードには、いくつかのミスがあります。

まず、id $("#showA")ではなく、$(".showA")クラスで要素を検索しようとしています。要素にはshowAクラスはありません。

第2に、$(document).ready終了タグが欠落しています。

また、リセット機能を処理するイベントも作成しました。

以下を参照してください。

$(document).ready(function(){ 
 

 
$("#showA").click(function() { 
 
    $('#house1').css('opacity', 1).fadeIn('slow'); 
 
}); 
 

 
$('#resetA').click(function(){ 
 
    $(this).parent('form').find('input[type="text"]').val(''); 
 
}); 
 
});
img 
 
{ 
 
\t padding:29px; 
 
\t opacity:0; 
 
\t width:250px; 
 
\t height:250px; 
 
} 
 

 
div 
 
{ 
 
    vertical-align:top; 
 
    width:267px; 
 
    height:auto; 
 
    display:inline-block; 
 
    padding:20px; 
 
    margin:0px;  
 
    border-radius:10px; 
 
    border:1px solid; 
 
} 
 

 
p 
 
{ 
 
\t margin:5px; 
 
\t padding:3px; 
 
\t background-color:blue; 
 
\t border-radius:8px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta charset="UTF-8" /> 
 
\t \t <link rel="stylesheet" type="text/css" href="css/finalproject.css"> 
 
\t \t <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
\t \t <script type="text/javascript" src="js/finalproject.js"> </script> 
 
\t \t <title>Final Project</title> 
 
\t </head> 
 
\t 
 
\t <body> 
 
\t \t <center><h1>Vacation Rental</h1></center> 
 
\t \t 
 
\t \t <div><form id="chatham"> 
 
     <label><b><u>Chatham, MA</u></b></label><br> 
 
     <label><b>$300 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameA" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailA" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneA" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
     <input id="showA" type="button" value="Show Picture"> 
 
     <input id="submitA" type="button" value="Submit"> 
 
     <input id="resetA" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="wellfleet"> 
 
     <label><b><u>Wellfleet, MA</u></b></label><br> 
 
     <label><b>$250 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameB" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailB" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneB" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showB" type="button" value="Show Picture"> 
 
     <input id="submitB" type="button" value="Submit"> 
 
     <input id="resetB" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="dennis"> 
 
     <label><b><u>Dennis, MA</u></b></label><br> 
 
     <label><b>$350 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameC" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailC" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneC" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showC" type="button" value="Show Picture"> 
 
     <input id="submitC" type="button" value="Submit"> 
 
     <input id="resetC" type="button" value="Reset"> 
 
     </form></div> 
 
     
 
     <div><form id="provincetown"> 
 
     <label><b><u>Provincetown, MA</u></b></label><br> 
 
     <label><b>$300 a Day</b></label><br> 
 
     <label>Name:</label><br> 
 
     <input name="nameD" type="text" /><br> 
 
     <label>Email Address:</label><br> 
 
     <input name="emailD" type="text" /><br> 
 
     <label>Phone Number:</label><br> 
 
     <input name="phoneD" type="text" /><br> 
 
     <label>How many days would you like to rent?</label><br> 
 
     <select> 
 
    \t \t \t <option value="5-10">5-10 Days</option> 
 
    \t \t \t <option value="6-7">10-15 Days</option> 
 
    \t \t \t <option value="8-9">15-20 Days</option> 
 
\t \t </select> 
 
\t \t <input id="showD" type="button" value="Show Picture"> 
 
     <input id="submitD" type="button" value="Submit"> 
 
     <input id="resetD" type="button" value="Reset"> 
 
     </form></div> 
 
    
 
\t <img id="house1" src="images/chatham.jpg" /> 
 
\t <img id="house2" src="images/wellfleet.jpg" /> 
 
\t <img id="house3" src="images/dennis.jpg" /> 
 
\t <img id="house4" src="images/provincetown.jpg" /> 
 
\t </body> 
 
</html>

関連する問題