2016-11-12 4 views
0

Ich habe eine HTML-TextBox auf meiner Webseite, die Nummer der aktuellen Frage (eine kleine Webseite, um eine Frage zu beantworten), ich möchte Verknüpfung zu einer Frage, die Benutzer wollen, nach Typ Anzahl von Frage in TextBox. Ich benutze diesen Code unten, aber das funktioniert nicht richtig. Foe Beispiel sind alle Fragen 8 und wenn ich 15 in TextBox eingeben und drücken Sie die Eingabetaste, wenn Klausel nicht funktioniert und Question Variable mit 15. Ich benutze Alert-Funktion, um es zu verfolgen, und ich verstehe, dass If-Klausel nicht korrekt funktionieren . Kann es jemand überprüfen und führen? Das ist alles von meinem Code:Machen Sie eine Berechnung in Javascript onkeypress

<?php 
$All = 8; 
$URL = "http://localhost/Test.php"; 
if(isset($_GET["edtQuestionNo"])){ 
    $QuestionNo = $_GET["edtQuestionNo"]; 
}else{ 
    $QuestionNo = 1; 
} 
?> 
<html> 
<head> 
<title>Test Page</title> 
<script type="text/javascript"> 
function KeyPress(e, URL, All){ 
    if(e.keyCode === 13){ 
     var Question = document.getElementsByName("edtQuestionNo")[0].value; 
     if(Question > All){ 
      Question = All; 
      alert(All + " " + Question + " yes"); 
     } 
     else{ 
      alert(All + " " + Question + " no"); 
     } 
     window.open(URL + "?edtQuestionNo=" + Question,"_self"); 
    } 
} 
</script> 
</head> 
<body> 
    <form action="Test.php" method="get" name="FRMQuestion"> 
     <label>Enter question number : </label> 
     <input type="text" name="edtQuestionNo" id="QuestionNo" value="<?php echo $QuestionNo; ?>" 
      onkeypress="KeyPress(event,'<?php echo $URL; ?>','<?php echo $All; ?>')"> 
     <br> 
     <label>Question number is : <?php echo $QuestionNo; ?></label> 
    </form> 
</body> 
</html> 
+0

zeigen, was in den Variablen „URL“ ist, „Alle“ und auch Ihre HTML-Container Sie das Tastendruck-Ereignis hinzufügen – repzero

+0

Nur ein Heads-up - dieser Code ist anfällig für XSS, wie Sie entgehen Benutzereingaben ($ QuestionNo) nicht, bevor sie gedruckt werden. Schauen Sie sich http://stackoverflow.com/questions/15755323/what-is-cross-site-scripting und http://stackoverflow.com/search?tab=votes&q=xss an. – Dogbert

Antwort

0

Ich löse es 1. Ich habe parseInt Funktion verwenden für alle und Frage Wert verglichen wird. weil sie in unterschiedlicher Art sind. 2. Ich setze den Fragewert (nach der Berechnung) erneut in HTML TextBox und öffne dann die URL. Mein Code ist:

<?php 
$All = 8; 
$URL = "http://localhost/Test.php"; 
if(isset($_GET["edtQuestionNo"])){ 
    $QuestionNo = $_GET["edtQuestionNo"]; 
}else{ 
    $QuestionNo = 1; 
} 
?> 
<html> 
<head> 
<title>Test Page</title> 
<script type="text/javascript"> 
function KeyPress(e, URL, All){ 
    if(e.keyCode === 13){ 
     var Question = document.getElementsByName("edtQuestionNo")[0].value; 
     if(parseInt(Question) > parseInt(All)){ 
      Question = All; 
     } 
     document.getElementsByName("edtQuestionNo")[0].value = Question; 
     window.open(URL + "?edtQuestionNo=" + Question,"_self"); 
    } 
} 
</script> 
</head> 
<body> 
    <form action="Test.php" method="get" name="FRMQuestion"> 
     <label>Enter question number : </label> 
     <input type="text" name="edtQuestionNo" id="QuestionNo" value="<?php echo $QuestionNo; ?>" 
      onkeypress="KeyPress(event,'<?php echo $URL; ?>','<?php echo $All; ?>')"> 
     <br> 
     <label>Question number is : <?php echo $QuestionNo; ?></label> 
    </form> 
</body> 
</html>