2017-04-26 226 views
-2

我試圖做到這一點,當我點擊「顯示圖片」按鈕,圖片將淡入,顯示房子的圖片。出於某種原因,我無法讓它爲我的生活工作。此外,我希望獲得重置按鈕以幫助清除表單。不知道爲什麼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')而不是$('#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

好了,有你的代碼的一些錯誤。

首先,您正在嘗試按類$(".showA")而不是編號$("#showA")查找元素。您的元素沒有showA類。

其次,您錯過了$(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>

相關問題