2017-04-26 9 views
-2

Ich versuche, dies zu machen, wenn ich auf die Schaltfläche "Bild anzeigen" klicke, wird ein Bild eingeblendet, das ein Bild des Hauses zeigt. Aus irgendeinem Grund kann ich es nicht für das Leben von mir arbeiten lassen. Außerdem möchte ich, dass die Reset-Schaltfläche funktioniert, um das Formular zu löschen.Kann nicht herausfinden, warum js nicht funktioniert

$(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

Mögliche Duplikat [Wie kann ich meine JavaScript-Code debuggen?] (Http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript- Code) – Liam

+0

Wenn Sie JS-Code ausführen. Ich würde empfehlen, indem Sie den Inspektor ('Strg + Shift + I') auf Chrom öffnen. Wenn JS fehlschlägt, wird dort der relative Fehler angezeigt, was das Debuggen erleichtert. Sehen Sie sich außerdem eine gute Entwicklungsumgebung (Texteditor/Online-Editor) an, die beim Schreiben mögliche Syntaxfehler (wie Ihre fehlenden Klammern) zeigt. – Francisco

Antwort

0

Sie vermissen einige Klammer zu schließen, finden Sie wie folgt vor:

$(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

Vielen Dank, aber die Funktion wird leider immer noch nicht ausgeführt. – kylebuzzy

0

Das Problem, dass Sie in Ihrem Code isttunstatt $('#showA'), weil es ein ID-Attribut ist, was Sie haben und auch überprüfen, dass Sie die opacity ändern müssen, um das Bild zu sehen.

$('#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

Vielen Dank! Gibt es eine Chance, dass Sie mir helfen können, es so zu machen, dass es wieder ausblendet, sobald der Knopf wieder gedrückt wird? Es ist ein Projekt für die Schule und ich muss eine If/Else-Anweisung verwenden und ich bin mir ziemlich sicher, dass es in dieser Situation verwendet werden kann. – kylebuzzy

+0

Die ursprüngliche Frage war, Ihren Code arbeiten zu lassen. Ich sehe nicht notwendig, 'if/else' Aussage hinzuzufügen. Können Sie die richtige Antwort wählen? –

0

Okay, gibt es ein paar Fehler auf dem Code.

Zuerst versuchen Sie, das Element nach Klasse $(".showA") anstelle von ID $("#showA") zu finden. Ihr Element hat keine showA Klasse.

Zweitens haben Sie verpasst $(document).ready schließende Tags.

Ich habe auch ein Ereignis gemacht, um die Reset-Funktion zu behandeln.

Siehe unten.

$(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>

Verwandte Themen