2016-05-30 3 views
-2

Im Schreiben von HTML-und JavaScript-Code für meine Website, was ich will ist ein Dropdown mit zwei Antworten, ja und nein. Wenn Ja ausgewählt ist, möchte ich ein verstecktes Formularfeld (und die Schaltfläche daneben) sichtbar und nutzbar. Mein Problem ist, dass der Code funktioniert, wenn ich eine sichtbare Form verberge, aber nicht umgekehrt (so, wie ich es will).HTML und Javascript: Machen Sie versteckte Formularfeld sichtbar mit einem if statment

Hier ist der Code, den ich verwende, die Art, wie ich dachte, es würde funktionieren. Sie sollten nicht über irgendetwas nach Funktion output kümmern müssen()

<html> 
 

 
<body> 
 

 
Are you a Patron? <br> 
 
<select id="patronAnswer"> 
 
    <option value="no">No</option> 
 
    <option value="yes">Yes</option> 
 
</select> <button name="name" type="button" onclick="patron()" id="patronSubmit">Submit</button> <br> 
 

 

 
<form id="form1"> 
 
<input name="name" type="text" size ="20" style="display:none" placeholder="Email used for patreon"> 
 
<button id="feildButton"type="button" onclick="outputname()"style="display:none">Submit</button> 
 
</form> 
 

 
<script> 
 
function patron() { 
 
    var patronUser = document.getElementById("patronAnswer"); 
 
    x=patronUser.options.selectedIndex 
 
    console.log (x) 
 
    if (x === 1) { 
 
    document.getElementById("form1").style.display="block"; 
 
    } 
 
} 
 

 
function outputname() { 
 
var email=document.getElementById("form1"); 
 
y=email.elements["name"].value; 
 
if (y==="[email protected]"|| 
 
    y==="[email protected]"|| 
 
    y==="bob"|| 
 
    y==="billy"){ 
 
    var down1 = confirm("You will be given the most current build"); 
 
    if (down1){ 
 
    var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67WlJhQUg0QWJMUFU" 
 
    var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
a.href = url; 
 
a.download = url.slice(url.lastIndexOf('/')+1); 
 
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
        false, false, false, false, 0, null); 
 
a.dispatchEvent(ev); 
 
    } 
 
    else if (down1===false) { 
 
    confirm ("download canceled") 
 
    } 
 
} 
 
    else { 
 
    var down2=confirm("You have not earned the level 2 reward, you will be given the public build."); 
 
    if (down2){ 
 
    var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67T3dUQUlOTmhYQXc" 
 
    var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
a.href = url; 
 
a.download = url.slice(url.lastIndexOf('/')+1); 
 
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
       false, false, false, false, 0, null); 
 
a.dispatchEvent(ev); 
 
    } 
 
    else if (down2===false){ 
 
    confirm ("download canceled") 
 
    } 
 
    } 
 
    } 
 

 

 

 
</script> 
 

 
<p id="demo"></p> 
 
</body> 
 

 
</html>

Hier ist ein Beispiel für eine, aber nicht in der Art und Weise funktioniert, die ich will. wieder sollte() nicht nach output um nichts kümmern müssen

<html> 
 

 
<body> 
 

 
Are you a Patron? <br> 
 
<select id="patronAnswer"> 
 
    <option value="no">No</option> 
 
    <option value="yes">Yes</option> 
 
</select> <button name="name" type="button" onclick="patron()" id="patronSubmit">Submit</button> <br> 
 

 

 
<form id="form1"> 
 
<input name="name" type="text" size ="20" style="display:block" placeholder="Email used for patreon"> 
 
<button id="feildButton"type="button" onclick="outputname()"style="display:none">Submit</button> 
 
</form> 
 

 
<script> 
 
function patron() { 
 
    var patronUser = document.getElementById("patronAnswer"); 
 
    x=patronUser.options.selectedIndex 
 
    console.log (x) 
 
    if (x === 1) { 
 
    document.getElementById("form1").style.display="none"; 
 
    } 
 
} 
 

 
function outputname() { 
 
var email=document.getElementById("form1"); 
 
y=email.elements["name"].value; 
 
if (y==="[email protected]"|| 
 
    y==="[email protected]"|| 
 
    y==="bob"|| 
 
    y==="billy"){ 
 
    var down1 = confirm("You will be given the most current build"); 
 
    if (down1){ 
 
    var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67WlJhQUg0QWJMUFU" 
 
    var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
a.href = url; 
 
a.download = url.slice(url.lastIndexOf('/')+1); 
 
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
        false, false, false, false, 0, null); 
 
a.dispatchEvent(ev); 
 
    } 
 
    else if (down1===false) { 
 
    confirm ("download canceled") 
 
    } 
 
} 
 
    else { 
 
    var down2=confirm("You have not earned the level 2 reward, you will be given the public build."); 
 
    if (down2){ 
 
    var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67T3dUQUlOTmhYQXc" 
 
    var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
a.href = url; 
 
a.download = url.slice(url.lastIndexOf('/')+1); 
 
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
       false, false, false, false, 0, null); 
 
a.dispatchEvent(ev); 
 
    } 
 
    else if (down2===false){ 
 
    confirm ("download canceled") 
 
    } 
 
    } 
 
    } 
 

 

 

 
</script> 
 

 
<p id="demo"></p> 
 
</body> 
 

 
</html>

Antwort

0

Ok, jetzt habe ich es, wie ich es will! Was Sie ursprünglich getan haben, arbeitete teilweise für das, was ich wollte. Das neue Problem war, dass wenn die Seite das Formular sichtbar lädt, ich nur eine zusätzliche Codezeile hinzufügte, die das Formular beim Laden der Seite verdeckte! Genau so wollte ich es! Danke für Ihre Hilfe!

Hier ist, was der neue Code sieht so aus:

<html> 
 

 
<body> 
 

 
Are you a Patron? <br> 
 
<select id="patronAnswer"> 
 
    <option value="no">No</option> 
 
    <option value="yes">Yes</option> 
 
</select> <button name="name" type="button" onclick="patron()" id="patronSubmit">Submit</button> <br> 
 

 

 
<form id="form1"> 
 
<input name="name" type="text" size ="20" style="display:block" placeholder="Email used for patreon"> 
 
<button id="feildButton"type="button" onclick="outputname()"style="display:block">Submit</button> 
 
</form> 
 

 
<script> 
 
document.getElementById("form1").style.visibility="hidden" 
 

 
function patron() { 
 
    var patronUser = document.getElementById("patronAnswer"); 
 
    x=patronUser.options.selectedIndex 
 
    console.log (x) 
 
    if (x === 1) { 
 
    document.getElementById("form1").style.visibility="visible"; 
 
    } 
 
    else{ 
 
    document.getElementById("form1").style.visibility="hidden"; 
 
    var down2=confirm("You have not earned the level 2 reward, you will be given the public build."); 
 
    if (down2){ 
 
     var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67T3dUQUlOTmhYQXc" 
 
     var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
    a.href = url; 
 
    a.download = url.slice(url.lastIndexOf('/')+1); 
 
    ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
        false, false, false, false, 0, null); 
 
    a.dispatchEvent(ev); 
 
    } 
 
    else if (down2===false){ 
 
     confirm ("download canceled") 
 
    } 
 
    } 
 
    } 
 

 
function outputname() { 
 
var email=document.getElementById("form1"); 
 
y=email.elements["name"].value; 
 
if (y==="[email protected]"|| 
 
    y==="[email protected]"|| 
 
    y==="bob"|| 
 
    y==="billy"){ 
 
    var down1 = confirm("You will be given the most current build"); 
 
    if (down1){ 
 
    var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67WlJhQUg0QWJMUFU" 
 
    var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
a.href = url; 
 
a.download = url.slice(url.lastIndexOf('/')+1); 
 
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
        false, false, false, false, 0, null); 
 
a.dispatchEvent(ev); 
 
    } 
 
    else if (down1===false) { 
 
    confirm ("download canceled") 
 
    } 
 
} 
 
    else { 
 
    var down2=confirm("You have not earned the level 2 reward, you will be given the public build."); 
 
    if (down2){ 
 
    var url = "https://drive.google.com/uc?export=download&id=0B8KkXdbkXf67T3dUQUlOTmhYQXc" 
 
    var a = document.createElement('a'), 
 
    ev = document.createEvent("MouseEvents"); 
 
a.href = url; 
 
a.download = url.slice(url.lastIndexOf('/')+1); 
 
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, 
 
       false, false, false, false, 0, null); 
 
a.dispatchEvent(ev); 
 
    } 
 
    else if (down2===false){ 
 
    confirm ("download canceled") 
 
    } 
 
    } 
 
    } 
 

 

 

 
</script> 
 

 
<p id="demo"></p> 
 
</body> 
 

 
</html>

Verwandte Themen