2017-05-15 8 views
-3

Ich versuche, eine angemessene Menge an Validierung mit indexOf zu erstellen.Javascript indexOf gibt kein Ergebnis zurück

save() ist ein onclick Attribut für eine Schaltfläche.
saveDirectionsForm ist die id einer Texteingabe.
result ist die id eines leeren Absatzes.

Dies ist mein Code so weit. Warum wird nichts gerendert, wenn ich auf save() klicke?

<!DOCTYPE html> 
<html> 
    <head> 
    <title>autoComplete</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="bootstrap.min.css" type="text/css"> 
    <link rel="stylesheet" href="autocomplete.css" type="text/css"> 
    <script src="jquery.min.js" type="text/javascript"></script> 
    </head> 
    <body> 
    <div id="masterControls"> 
    <input name="origin-input" id="origin-input" class="controls" type="text" 
     placeholder="Enter an origin location"> 
    <input name="destination-input" id="destination-input" class="controls" type="text" 
     placeholder="Enter a destination location"> 

    <div id="mode-selector" class="controls"> 
     <input type="radio" name="type" id="changemode-walking"> 
     <label for="changemode-walking">Walking</label> 

     <input type="radio" name="type" id="changemode-bicycling" checked="checked"> 
     <label for="changemode-bicycling">Bicycling</label> 

     <!-- <input type="radio" name="type" id="changemode-transit"> 
     <label for="changemode-transit">Transit</label> --> 

     <input type="radio" name="type" id="changemode-driving"> 
     <label for="changemode-driving">Driving</label>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp; 

     <label for="tolls">Avoid Tolls: </label> 
     <input id="tolls" type="checkbox" name="tolls" onchange="switchTolls(this)" checked="checked">&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp; 

     <button id="restartMap" class="btn btn-default btn-xs" style="height:80%;top:0;margin-bottom:6px;" onclick="restartMap()" type="button">Restart</button> 
    </div> 
    </div> 

    <div id="saveMapAlert" style="display:none;" class="alert alert-success alert-dismissible" role="alert"> 
    <button id="saveMapAlertClose" type="button" class="close" data-dismiss="alert" aria-label="Close"> 
     <span aria-hidden="true">&times;</span> 
    </button> 
    <strong>Nice route!</strong> Do you want to save these directions?&nbsp;&nbsp; <span class="label label-default" id="yesAlert" onclick="yesAlert()">Yes!</span> <span class="label label-danger" id="noAlert" onclick="noAlert()">No!</span> 
    </div> 

    <div id="saveDirectionsWrapper" class="col-lg-6" style="display:none;margin-bottom:20px;width:25%;"> 
    <div class="input-group"> 
     <input type="text" class="form-control" placeholder="Email Address" required="required" /> 
     <span class="input-group-btn"> 
     <button id="saveDirectionsForm" onclick="save()" class="btn btn-default" type="button">Send!</button> 
     <!-- <button id="saveDirectionsForm" onclick="saveDirectionsForm()" class="btn btn-default" type="button"><span class="glyphicon glyphicon-ok"></span></button> --> 
     </span> 
    </div><!-- input-group --> 
    </div><!-- col-lg-6 && saveDirectionsWrapper --> 

    <div id="saveMapAlert2" style="display:none;" class="alert alert-danger alert-dismissible" role="alert"> 
    <button id="saveMapAlertClose2" type="button" class="close" data-dismiss="alert" aria-label="Close"> 
     <span aria-hidden="true">&times;</span> 
    </button> 
    <strong>Oh snap!</strong> Blah blah! 
    </div> 

    <p id="result"></p> 

    <div id="map"></div> 
    <div id="panelWrapper"> 
     <div id="panel" style="height:100%;width:100%;float:right;"></div> 
    </div> 

<script> 

    function save() { 
     var emailString = document.getElementById('saveDirectionsForm').value; 
     if (emailString.indexOf("@") > 0) { 
      document.getElementById("result").innerHTML = "YES"; 
     } 
     }; 
</script> 

+1

Welchen Wert überprüfen Sie? – Grundy

+0

'saveDirectionsForm' scheint wie dies ist eine Formular-ID, müssen Sie die E-Mail-Feld-ID verwenden –

+0

ist die ID des Feldes INPUT: ** saveDirectionsForm **? –

Antwort

-1
<button id="saveDirectionsForm" onclick="save()" class="btn btn-default" type="button">Send!</button> 

Das sieht seltsam, versuchen Sie, den Wert einer Taste zu bekommen, wird es nicht funktionieren, ist nichts falsch mit Ihrem indexOf Methode,

//you are getting it wrong here 
     var emailString =  document.getElementById('saveDirectionsForm').value; 
+0

Entschuldigung dafür, dass die Benennung der Schaltfläche und der Texteingabe nicht organisiert ist. Danke für Ihre Rückmeldung. – classicSchmosby98

+0

Gern geschehen –

Verwandte Themen